From a573cc199a00d35410197ba4117c97764a984dc0 Mon Sep 17 00:00:00 2001 From: TK Date: Thu, 28 Mar 2024 14:16:18 -0400 Subject: [PATCH] feat: allow dynamic route segments in isr.exclude array (#10513) * Allow dynamic route segments in isr.exclude array * copy over eslint-disable as well * add test * update test: slashes dont need to be escaped * update changeset --------- Co-authored-by: lilnasy <69170106+lilnasy@users.noreply.github.com> --- .changeset/angry-lamps-cheer.md | 17 +++++++++ .../integrations/vercel/src/lib/redirects.ts | 37 ++++++++++++++++++- .../vercel/test/fixtures/isr/astro.config.mjs | 2 +- .../isr/src/pages/excluded/[dynamic].astro | 8 ++++ packages/integrations/vercel/test/isr.test.js | 8 ++++ 5 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 .changeset/angry-lamps-cheer.md create mode 100644 packages/integrations/vercel/test/fixtures/isr/src/pages/excluded/[dynamic].astro diff --git a/.changeset/angry-lamps-cheer.md b/.changeset/angry-lamps-cheer.md new file mode 100644 index 0000000000..d8bdee8139 --- /dev/null +++ b/.changeset/angry-lamps-cheer.md @@ -0,0 +1,17 @@ +--- +"@astrojs/vercel": minor +--- + +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]", + ] + } + }) +}) +``` diff --git a/packages/integrations/vercel/src/lib/redirects.ts b/packages/integrations/vercel/src/lib/redirects.ts index 8f8db4dcf8..1e476cb1f2 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,33 @@ interface VercelRoute { continue?: boolean; } +// 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) { + 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 +104,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[] { diff --git a/packages/integrations/vercel/test/fixtures/isr/astro.config.mjs b/packages/integrations/vercel/test/fixtures/isr/astro.config.mjs index e57e6bb7ee..4b675ab6c7 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 0000000000..54ba67fa86 --- /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 644d7e1f54..c87740df89 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',