From a1c57c939c105c09ae26e77696b21f7303a90be1 Mon Sep 17 00:00:00 2001 From: Luiz Ferraz Date: Tue, 26 Mar 2024 22:23:47 -0300 Subject: [PATCH] [pkg/sitemap-ext]: Fix static SSR pages double entries --- .changeset/perfect-mails-bathe.md | 5 +++++ packages/sitemap-ext/index.ts | 27 ++++++++++++++++++++------- 2 files changed, 25 insertions(+), 7 deletions(-) create mode 100644 .changeset/perfect-mails-bathe.md diff --git a/.changeset/perfect-mails-bathe.md b/.changeset/perfect-mails-bathe.md new file mode 100644 index 00000000..50857490 --- /dev/null +++ b/.changeset/perfect-mails-bathe.md @@ -0,0 +1,5 @@ +--- +"@inox-tools/sitemap-ext": patch +--- + +Fixes bug causing duplicated SSR entries with static paths on the sitemap diff --git a/packages/sitemap-ext/index.ts b/packages/sitemap-ext/index.ts index 6d7feacd..92fabe6a 100644 --- a/packages/sitemap-ext/index.ts +++ b/packages/sitemap-ext/index.ts @@ -33,7 +33,7 @@ export default defineIntegration({ setup: ({ options: { includeByDefault, customPages: _externalPages, ...options } }) => { type InclusionRule = | { type: 'regex'; regex: RegExp; decision: boolean } - | { type: 'static'; path: string; decision: boolean }; + | { type: 'static'; path: string; comparePath: string; decision: boolean; static: boolean }; const inclusions: InclusionRule[] = []; function makeDecision( @@ -48,11 +48,23 @@ export default defineIntegration({ for (const routeParam of routeParams) { const pathName = route.generate(routeParam); - inclusions.push({ type: 'static', path: onlyLeadingSlash(pathName), decision }); + inclusions.push({ + type: 'static', + path: pathName, + comparePath: onlyLeadingSlash(pathName), + decision, + static: false, + }); } } } else { - inclusions.push({ type: 'static', path: onlyLeadingSlash(route.pathname), decision }); + inclusions.push({ + type: 'static', + path: route.pathname, + comparePath: onlyLeadingSlash(route.pathname), + decision, + static: true, + }); } } @@ -83,7 +95,7 @@ export default defineIntegration({ defineRouteConfig({ importName: 'sitemap-ext:config', - callbackHandler: (context, configCb: ConfigCallback | boolean) => { + callbackHandler: async (context, configCb: ConfigCallback | boolean) => { const hooks: Parameters[0] = { removeFromSitemap(routeParams) { for (const route of context.routeData) { @@ -112,7 +124,7 @@ export default defineIntegration({ hooks.removeFromSitemap(); } } else { - configCb(hooks); + await configCb(hooks); } }, }); @@ -131,7 +143,7 @@ export default defineIntegration({ const ruling = inclusions.find( (r) => - (r.type === 'static' && r.path === route) || + (r.type === 'static' && r.comparePath === route) || (r.type === 'regex' && r.regex.test(route)) ); @@ -146,7 +158,8 @@ export default defineIntegration({ const extraPagesSet = new Set( inclusions .filter( - (i): i is InclusionRule & { type: 'static' } => i.type === 'static' && i.decision + (i): i is InclusionRule & { type: 'static' } => + i.type === 'static' && i.decision && !i.static ) .map((i) => trimSlashes(i.path)) );