Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pkg/sitemap-ext]: Fixes handling of projects with a base with a trailing slash #61

Merged
merged 3 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
5 changes: 5 additions & 0 deletions .changeset/strange-pugs-rush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@inox-tools/aik-route-config": patch
---

Adds a log message on errors to import SSR modules during build time
5 changes: 5 additions & 0 deletions .changeset/weak-sloths-trade.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@inox-tools/sitemap-ext": patch
---

Fixes handling of projects with a base with a trailing slash
7 changes: 5 additions & 2 deletions packages/aik-route-config/src/contextResolution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Once } from './once.js';
import { defineIntegration, addIntegration, addVitePlugin } from 'astro-integration-kit';
import { fileURLToPath } from 'node:url';
import { normalizePath } from 'vite';
import { inspect } from 'node:util';

export type ConfigContext = {
route: string[];
Expand Down Expand Up @@ -73,7 +74,7 @@ const integration = defineIntegration({
}
}
},
'astro:build:ssr': async ({ manifest: { routes } }) => {
'astro:build:ssr': async ({ logger, manifest: { routes } }) => {
const ssrComponents = routes
.map((r) => r.routeData)
.filter((r) => r.type === 'page')
Expand All @@ -84,7 +85,9 @@ const integration = defineIntegration({

// Import SSR components so the hoisted logic gets executed
for (const module of ssrComponents) {
await import(/* @vite-ignore */ module!).catch(() => {});
await import(/* @vite-ignore */ module!).catch((error) => {
logger.error(`Failed to import SSR component: ${module!} ${inspect(error)}`);
});
}
},
};
Expand Down
39 changes: 30 additions & 9 deletions packages/sitemap-ext/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { z } from 'astro/zod';
import * as path from 'node:path';
import sitemap from '@astrojs/sitemap';
import './virtual.d.ts';
import { inspect } from 'node:util';

process.setSourceMapsEnabled(true);

Expand All @@ -32,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 @@ -47,11 +48,23 @@ export default defineIntegration({
for (const routeParam of routeParams) {
const pathName = route.generate(routeParam);

inclusions.push({ type: 'static', path: pathName, decision });
inclusions.push({
type: 'static',
path: pathName,
comparePath: onlyLeadingSlash(pathName),
decision,
static: false,
});
}
}
} else {
inclusions.push({ type: 'static', path: route.pathname, decision });
inclusions.push({
type: 'static',
path: route.pathname,
comparePath: onlyLeadingSlash(route.pathname),
decision,
static: true,
});
}
}

Expand All @@ -61,7 +74,7 @@ export default defineIntegration({

return {
'astro:config:setup': (params) => {
const { defineRouteConfig, config } = params;
const { defineRouteConfig, logger, config } = params;

if (hasIntegration(params, { name: '@astrojs/sitemap' })) {
throw new AstroError(
Expand All @@ -82,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 All @@ -103,14 +116,15 @@ export default defineIntegration({
},
};

logger.debug('Running sitemap config callback:' + inspect({ context, configCb }));
if (typeof configCb === 'boolean') {
if (configCb) {
hooks.addToSitemap();
} else {
hooks.removeFromSitemap();
}
} else {
configCb(hooks);
await configCb(hooks);
}
},
});
Expand All @@ -125,14 +139,16 @@ export default defineIntegration({
customPages: extraPages,
filter: (page) => {
const url = new URL(page);
const route = path.relative(config.base, url.pathname);
const route = onlyLeadingSlash(path.relative(config.base, url.pathname));

const ruling = inclusions.find(
(r) =>
(r.type === 'static' && r.path === route) ||
(r.type === 'static' && r.comparePath === route) ||
(r.type === 'regex' && r.regex.test(route))
);

logger.debug(`Ruling for ${route}: ${inspect(ruling ?? includeByDefault)}`);

return ruling?.decision ?? includeByDefault;
},
}),
Expand All @@ -142,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 All @@ -162,3 +179,7 @@ export default defineIntegration({
function trimSlashes(input: string): string {
return input.replace(/^\/+|\/+$/g, '');
}

function onlyLeadingSlash(input: string): string {
return '/' + trimSlashes(input);
}
4 changes: 3 additions & 1 deletion packages/sitemap-ext/tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ export default defineConfig({
'./virtual.d.ts',
'vite',
],
noExternal: [],
noExternal: [
// '@inox-tools/aik-route-config',
],
treeshake: 'smallest',
tsconfig: 'tsconfig.json',
});
Loading