mirror of
https://github.com/withastro/astro.git
synced 2025-02-17 22:44:24 -05:00
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>
This commit is contained in:
parent
c0cae63147
commit
a573cc199a
5 changed files with 69 additions and 3 deletions
17
.changeset/angry-lamps-cheer.md
Normal file
17
.changeset/angry-lamps-cheer.md
Normal file
|
@ -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]",
|
||||||
|
]
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
```
|
|
@ -1,5 +1,5 @@
|
||||||
import nodePath from 'node:path';
|
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';
|
import type { AstroConfig, RouteData, RoutePart } from 'astro';
|
||||||
|
|
||||||
const pathJoin = nodePath.posix.join;
|
const pathJoin = nodePath.posix.join;
|
||||||
|
@ -14,6 +14,33 @@ interface VercelRoute {
|
||||||
continue?: boolean;
|
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
|
// Copied from /home/juanm04/dev/misc/astro/packages/astro/src/core/routing/manifest/create.ts
|
||||||
// 2022-04-26
|
// 2022-04-26
|
||||||
function getMatchPattern(segments: RoutePart[][]) {
|
function getMatchPattern(segments: RoutePart[][]) {
|
||||||
|
@ -77,7 +104,13 @@ function getRedirectStatus(route: RouteData): number {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function escapeRegex(content: string) {
|
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[] {
|
export function getRedirects(routes: RouteData[], config: AstroConfig): VercelRoute[] {
|
||||||
|
|
|
@ -7,7 +7,7 @@ export default defineConfig({
|
||||||
isr: {
|
isr: {
|
||||||
bypassToken: "1c9e601d-9943-4e7c-9575-005556d774a8",
|
bypassToken: "1c9e601d-9943-4e7c-9575-005556d774a8",
|
||||||
expiration: 120,
|
expiration: 120,
|
||||||
exclude: ["/two"]
|
exclude: ["/two", "/excluded/[dynamic]"]
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
8
packages/integrations/vercel/test/fixtures/isr/src/pages/excluded/[dynamic].astro
vendored
Normal file
8
packages/integrations/vercel/test/fixtures/isr/src/pages/excluded/[dynamic].astro
vendored
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Dynamic</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>Dynamic</h1>
|
||||||
|
</body>
|
||||||
|
</html>
|
|
@ -33,10 +33,18 @@ describe('ISR', () => {
|
||||||
src: '^/two$',
|
src: '^/two$',
|
||||||
dest: '_render',
|
dest: '_render',
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
src: '^/excluded/([^/]+?)$',
|
||||||
|
dest: '_render'
|
||||||
|
},
|
||||||
{
|
{
|
||||||
src: '^\\/_image$',
|
src: '^\\/_image$',
|
||||||
dest: '_render',
|
dest: '_render',
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
src: '^\\/excluded\\/([^/]+?)\\/?$',
|
||||||
|
dest: '/_isr?x_astro_path=$0',
|
||||||
|
},
|
||||||
{
|
{
|
||||||
src: '^\\/one\\/?$',
|
src: '^\\/one\\/?$',
|
||||||
dest: '/_isr?x_astro_path=$0',
|
dest: '/_isr?x_astro_path=$0',
|
||||||
|
|
Loading…
Add table
Reference in a new issue