From 431c9afb1dfcbddbd19ecb9488c9cd2505068d93 Mon Sep 17 00:00:00 2001 From: tk04 Date: Wed, 20 Mar 2024 23:30:35 -0400 Subject: [PATCH 1/5] Allow dynamic route segments in isr.exclude array --- .changeset/angry-lamps-cheer.md | 5 +++ .../integrations/vercel/src/lib/redirects.ts | 35 +++++++++++++++++-- 2 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 .changeset/angry-lamps-cheer.md diff --git a/.changeset/angry-lamps-cheer.md b/.changeset/angry-lamps-cheer.md new file mode 100644 index 000000000000..eb3543f2f304 --- /dev/null +++ b/.changeset/angry-lamps-cheer.md @@ -0,0 +1,5 @@ +--- +"@astrojs/vercel": minor +--- + +Allow dynamic route segments in isr.exclude array diff --git a/packages/integrations/vercel/src/lib/redirects.ts b/packages/integrations/vercel/src/lib/redirects.ts index 8f8db4dcf888..0632a65bf198 100644 --- a/packages/integrations/vercel/src/lib/redirects.ts +++ b/packages/integrations/vercel/src/lib/redirects.ts @@ -1,5 +1,5 @@ import nodePath from 'node:path'; -import { appendForwardSlash } from '@astrojs/internal-helpers/path'; +import { appendForwardSlash, removeLeadingForwardSlash } from '@astrojs/internal-helpers/path'; import type { AstroConfig, RouteData, RoutePart } from 'astro'; const pathJoin = nodePath.posix.join; @@ -14,6 +14,31 @@ interface VercelRoute { continue?: boolean; } +// Copied from astro/packages/astro/src/core/routing/manifest/create.ts +const ROUTE_DYNAMIC_SPLIT = /\[(.+?\(.+?\)|.+?)\]/; +const ROUTE_SPREAD = /^\.{3}.+$/; +function getParts(part: string, file: string) { + const result: RoutePart[] = []; + part.split(ROUTE_DYNAMIC_SPLIT).map((str, i) => { + if (!str) return; + const dynamic = i % 2 === 1; + + const [, content] = dynamic ? /([^(]+)$/.exec(str) || [null, null] : [null, str]; + + if (!content || (dynamic && !/^(?:\.\.\.)?[\w$]+$/.test(content))) { + throw new Error(`Invalid route ${file} — parameter name must match /^[a-zA-Z0-9_$]+$/`); + } + + result.push({ + content, + dynamic, + spread: dynamic && ROUTE_SPREAD.test(content), + }); + }); + + return result; +} + // Copied from /home/juanm04/dev/misc/astro/packages/astro/src/core/routing/manifest/create.ts // 2022-04-26 function getMatchPattern(segments: RoutePart[][]) { @@ -77,7 +102,13 @@ function getRedirectStatus(route: RouteData): number { } export function escapeRegex(content: string) { - return `^${getMatchPattern([[{ content, dynamic: false, spread: false }]])}$`; + const segments = removeLeadingForwardSlash(content) + .split(nodePath.posix.sep) + .filter(Boolean) + .map((s: string) => { + return getParts(s, content); + }); + return `^/${getMatchPattern(segments)}$`; } export function getRedirects(routes: RouteData[], config: AstroConfig): VercelRoute[] { From dd423a0168bb707e431f8e8f8759a2e231018160 Mon Sep 17 00:00:00 2001 From: lilnasy <69170106+lilnasy@users.noreply.github.com> Date: Wed, 27 Mar 2024 21:12:42 +0000 Subject: [PATCH 2/5] copy over eslint-disable as well --- packages/integrations/vercel/src/lib/redirects.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/integrations/vercel/src/lib/redirects.ts b/packages/integrations/vercel/src/lib/redirects.ts index 0632a65bf198..1e476cb1f288 100644 --- a/packages/integrations/vercel/src/lib/redirects.ts +++ b/packages/integrations/vercel/src/lib/redirects.ts @@ -15,6 +15,8 @@ interface VercelRoute { } // Copied from astro/packages/astro/src/core/routing/manifest/create.ts +// Disable eslint as we're not sure how to improve this regex yet +// eslint-disable-next-line regexp/no-super-linear-backtracking const ROUTE_DYNAMIC_SPLIT = /\[(.+?\(.+?\)|.+?)\]/; const ROUTE_SPREAD = /^\.{3}.+$/; function getParts(part: string, file: string) { From bc815b99505d665d2b4bbb605dd8ad7934bd73c0 Mon Sep 17 00:00:00 2001 From: lilnasy <69170106+lilnasy@users.noreply.github.com> Date: Wed, 27 Mar 2024 21:25:03 +0000 Subject: [PATCH 3/5] add test --- .../vercel/test/fixtures/isr/astro.config.mjs | 2 +- .../test/fixtures/isr/src/pages/excluded/[dynamic].astro | 8 ++++++++ packages/integrations/vercel/test/isr.test.js | 8 ++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 packages/integrations/vercel/test/fixtures/isr/src/pages/excluded/[dynamic].astro diff --git a/packages/integrations/vercel/test/fixtures/isr/astro.config.mjs b/packages/integrations/vercel/test/fixtures/isr/astro.config.mjs index e57e6bb7eec5..4b675ab6c7b2 100644 --- a/packages/integrations/vercel/test/fixtures/isr/astro.config.mjs +++ b/packages/integrations/vercel/test/fixtures/isr/astro.config.mjs @@ -7,7 +7,7 @@ export default defineConfig({ isr: { bypassToken: "1c9e601d-9943-4e7c-9575-005556d774a8", expiration: 120, - exclude: ["/two"] + exclude: ["/two", "/excluded/[dynamic]"] } }) }); diff --git a/packages/integrations/vercel/test/fixtures/isr/src/pages/excluded/[dynamic].astro b/packages/integrations/vercel/test/fixtures/isr/src/pages/excluded/[dynamic].astro new file mode 100644 index 000000000000..54ba67fa8640 --- /dev/null +++ b/packages/integrations/vercel/test/fixtures/isr/src/pages/excluded/[dynamic].astro @@ -0,0 +1,8 @@ + + + Dynamic + + +

Dynamic

+ + diff --git a/packages/integrations/vercel/test/isr.test.js b/packages/integrations/vercel/test/isr.test.js index 644d7e1f54a1..8c2a91f626ce 100644 --- a/packages/integrations/vercel/test/isr.test.js +++ b/packages/integrations/vercel/test/isr.test.js @@ -33,10 +33,18 @@ describe('ISR', () => { src: '^/two$', dest: '_render', }, + { + src: '^\\/excluded\\/([^/]+?)$', + dest: '_render' + }, { src: '^\\/_image$', dest: '_render', }, + { + src: '^\\/excluded\\/([^/]+?)\\/?$', + dest: '/_isr?x_astro_path=$0', + }, { src: '^\\/one\\/?$', dest: '/_isr?x_astro_path=$0', From d7e29157e65a372bf01ca8da5f169c07b1857297 Mon Sep 17 00:00:00 2001 From: lilnasy <69170106+lilnasy@users.noreply.github.com> Date: Wed, 27 Mar 2024 21:48:51 +0000 Subject: [PATCH 4/5] update test: slashes dont need to be escaped --- packages/integrations/vercel/test/isr.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/integrations/vercel/test/isr.test.js b/packages/integrations/vercel/test/isr.test.js index 8c2a91f626ce..c87740df8903 100644 --- a/packages/integrations/vercel/test/isr.test.js +++ b/packages/integrations/vercel/test/isr.test.js @@ -34,7 +34,7 @@ describe('ISR', () => { dest: '_render', }, { - src: '^\\/excluded\\/([^/]+?)$', + src: '^/excluded/([^/]+?)$', dest: '_render' }, { From 4ad07a0ceda222d738fca7f58f470915b0ad283d Mon Sep 17 00:00:00 2001 From: lilnasy <69170106+lilnasy@users.noreply.github.com> Date: Thu, 28 Mar 2024 18:15:39 +0000 Subject: [PATCH 5/5] update changeset --- .changeset/angry-lamps-cheer.md | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/.changeset/angry-lamps-cheer.md b/.changeset/angry-lamps-cheer.md index eb3543f2f304..d8bdee813926 100644 --- a/.changeset/angry-lamps-cheer.md +++ b/.changeset/angry-lamps-cheer.md @@ -2,4 +2,16 @@ "@astrojs/vercel": minor --- -Allow dynamic route segments in isr.exclude array +The `isr.exclude` configuration can now include routes with dynamic and spread parameters. +```ts +export default defineConfig({ + adapter: vercel({ + isr: { + exclude: [ + "/blog/[title]" + "/api/[...slug]", + ] + } + }) +}) +```