From 95e2e6d372f6966044fa46e9beb0289678e3d98e Mon Sep 17 00:00:00 2001 From: Luiz Ferraz Date: Tue, 26 Mar 2024 21:14:48 -0300 Subject: [PATCH 1/3] [pkg/sitemap-ext]: Fixes handling of projects with a base with a trailing slash --- .changeset/weak-sloths-trade.md | 5 +++++ packages/sitemap-ext/index.ts | 16 ++++++++++++---- packages/sitemap-ext/tsup.config.ts | 4 +++- 3 files changed, 20 insertions(+), 5 deletions(-) create mode 100644 .changeset/weak-sloths-trade.md diff --git a/.changeset/weak-sloths-trade.md b/.changeset/weak-sloths-trade.md new file mode 100644 index 00000000..22cd2b3b --- /dev/null +++ b/.changeset/weak-sloths-trade.md @@ -0,0 +1,5 @@ +--- +"@inox-tools/sitemap-ext": patch +--- + +Fixes handling of projects with a base with a trailing slash diff --git a/packages/sitemap-ext/index.ts b/packages/sitemap-ext/index.ts index 38d2d4bc..6d7feacd 100644 --- a/packages/sitemap-ext/index.ts +++ b/packages/sitemap-ext/index.ts @@ -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); @@ -47,11 +48,11 @@ 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: onlyLeadingSlash(pathName), decision }); } } } else { - inclusions.push({ type: 'static', path: route.pathname, decision }); + inclusions.push({ type: 'static', path: onlyLeadingSlash(route.pathname), decision }); } } @@ -61,7 +62,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( @@ -103,6 +104,7 @@ export default defineIntegration({ }, }; + logger.debug('Running sitemap config callback:' + inspect({ context, configCb })); if (typeof configCb === 'boolean') { if (configCb) { hooks.addToSitemap(); @@ -125,7 +127,7 @@ 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) => @@ -133,6 +135,8 @@ export default defineIntegration({ (r.type === 'regex' && r.regex.test(route)) ); + logger.debug(`Ruling for ${route}: ${inspect(ruling ?? includeByDefault)}`); + return ruling?.decision ?? includeByDefault; }, }), @@ -162,3 +166,7 @@ export default defineIntegration({ function trimSlashes(input: string): string { return input.replace(/^\/+|\/+$/g, ''); } + +function onlyLeadingSlash(input: string): string { + return '/' + trimSlashes(input); +} diff --git a/packages/sitemap-ext/tsup.config.ts b/packages/sitemap-ext/tsup.config.ts index 9f5b7dd6..eb6cdb35 100644 --- a/packages/sitemap-ext/tsup.config.ts +++ b/packages/sitemap-ext/tsup.config.ts @@ -20,7 +20,9 @@ export default defineConfig({ './virtual.d.ts', 'vite', ], - noExternal: [], + noExternal: [ + // '@inox-tools/aik-route-config', + ], treeshake: 'smallest', tsconfig: 'tsconfig.json', }); From 7e93e79e2388ffd764fde602de84f1b55e405ad8 Mon Sep 17 00:00:00 2001 From: Luiz Ferraz Date: Tue, 26 Mar 2024 21:15:57 -0300 Subject: [PATCH 2/3] [pkg/aik-route-config]: Adds a log message on SSR import errors --- .changeset/strange-pugs-rush.md | 5 +++++ packages/aik-route-config/src/contextResolution.ts | 7 +++++-- 2 files changed, 10 insertions(+), 2 deletions(-) create mode 100644 .changeset/strange-pugs-rush.md diff --git a/.changeset/strange-pugs-rush.md b/.changeset/strange-pugs-rush.md new file mode 100644 index 00000000..e636dc5e --- /dev/null +++ b/.changeset/strange-pugs-rush.md @@ -0,0 +1,5 @@ +--- +"@inox-tools/aik-route-config": patch +--- + +Adds a log message on errors to import SSR modules during build time diff --git a/packages/aik-route-config/src/contextResolution.ts b/packages/aik-route-config/src/contextResolution.ts index e120167c..458893a6 100644 --- a/packages/aik-route-config/src/contextResolution.ts +++ b/packages/aik-route-config/src/contextResolution.ts @@ -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[]; @@ -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') @@ -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)}`); + }); } }, }; From 9a9fd73625c07e74bebb09e3c8dadde383e2a339 Mon Sep 17 00:00:00 2001 From: Luiz Ferraz Date: Tue, 26 Mar 2024 22:23:47 -0300 Subject: [PATCH 3/3] [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)) );