Skip to content

Commit e942dae

Browse files
committed
fix(trustlab): ensure generateURL always returns an absolute URL
Extracts generateURL into a named function with correct destructured signature ({ doc }), strips the leading slash from pathname to pair with site.url's trailing slash, and falls back to slug when pathname is absent.
1 parent a7df942 commit e942dae

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

  • apps/trustlab/src/payload/plugins

apps/trustlab/src/payload/plugins/index.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ const s3MaxAttempts = Number(process.env.S3_MAX_ATTEMPTS) || 3;
1616
const s3MaxSockets = Number(process.env.S3_MAX_SOCKETS) || 1000;
1717
const s3ConnectionTimeout = Number(process.env.S3_CONNECTION_TIMEOUT) || 5000;
1818

19+
function generateURL({ doc }) {
20+
if (!(doc?.pathname || doc?.slug)) {
21+
return "";
22+
}
23+
// site.url always has a trailing /
24+
const pathname = (doc.pathname || `${doc.slug}`).replace(/^\//, "");
25+
return `${site.url}${pathname}`;
26+
}
27+
1928
const plugins = [
2029
nestedDocsPlugin({
2130
collections: ["pages", "posts"],
@@ -80,8 +89,7 @@ const plugins = [
8089
},
8190
// Organisations don't have title, just name
8291
generateTitle: ({ doc }) => doc?.title ?? doc?.name ?? "",
83-
generateURL: ({ doc }) =>
84-
doc?.pathname ?? (doc?.slug ? `${site.url}${doc.slug}` : ""),
92+
generateURL,
8593
uploadsCollection: "media",
8694
}),
8795
];

0 commit comments

Comments
 (0)