Skip to content

Commit

Permalink
[pkg/sitemap-ext]: Fix static SSR pages double entries
Browse files Browse the repository at this point in the history
  • Loading branch information
Fryuni committed Mar 27, 2024
1 parent 1a47034 commit a1c57c9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/perfect-mails-bathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@inox-tools/sitemap-ext": patch
---

Fixes bug causing duplicated SSR entries with static paths on the sitemap
27 changes: 20 additions & 7 deletions packages/sitemap-ext/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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,
});
}
}

Expand Down Expand Up @@ -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<ConfigCallback>[0] = {
removeFromSitemap(routeParams) {
for (const route of context.routeData) {
Expand Down Expand Up @@ -112,7 +124,7 @@ export default defineIntegration({
hooks.removeFromSitemap();
}
} else {
configCb(hooks);
await configCb(hooks);
}
},
});
Expand All @@ -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))
);

Expand All @@ -146,7 +158,8 @@ export default defineIntegration({
const extraPagesSet = new Set<string>(
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))
);
Expand Down

0 comments on commit a1c57c9

Please sign in to comment.