Skip to content

Commit

Permalink
feat: allow dynamic route segments in isr.exclude array (#10513)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
tk04 and lilnasy committed Mar 28, 2024
1 parent c0cae63 commit a573cc1
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 3 deletions.
17 changes: 17 additions & 0 deletions .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]",
]
}
})
})
```
37 changes: 35 additions & 2 deletions 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;
Expand All @@ -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[][]) {
Expand Down Expand Up @@ -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[] {
Expand Down
Expand Up @@ -7,7 +7,7 @@ export default defineConfig({
isr: {
bypassToken: "1c9e601d-9943-4e7c-9575-005556d774a8",
expiration: 120,
exclude: ["/two"]
exclude: ["/two", "/excluded/[dynamic]"]
}
})
});
@@ -0,0 +1,8 @@
<html>
<head>
<title>Dynamic</title>
</head>
<body>
<h1>Dynamic</h1>
</body>
</html>
8 changes: 8 additions & 0 deletions packages/integrations/vercel/test/isr.test.js
Expand Up @@ -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',
Expand Down

0 comments on commit a573cc1

Please sign in to comment.