Skip to content

Commit

Permalink
feat(vercel): allow defining ISR exclusions with regular expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
slawekkolodziej committed Feb 10, 2025
1 parent 9d56602 commit 385b447
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 22 deletions.
38 changes: 30 additions & 8 deletions packages/integrations/vercel/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ interface VercelISRConfig {
*
* @default `[]`
*/
exclude?: string[];
exclude?: (string | RegExp)[];
}

export default function vercelAdapter({
Expand Down Expand Up @@ -407,20 +407,42 @@ export default function vercelAdapter({
const isrConfig = typeof isr === 'object' ? isr : {};
await builder.buildServerlessFolder(entryFile, NODE_PATH, _config.root);
if (isrConfig.exclude?.length) {
const expandedExclusions = isrConfig.exclude.reduce<string[]>((acc, exclusion) => {
if (exclusion instanceof RegExp) {
return [
...acc,
...routes.filter(route => exclusion.test(route.pattern)).map(route => route.pattern)
]
}

return [...acc, exclusion];
}, []);

const dest = _middlewareEntryPoint ? MIDDLEWARE_PATH : NODE_PATH;
for (const route of isrConfig.exclude) {
for (const route of expandedExclusions) {
// vercel interprets src as a regex pattern, so we need to escape it
routeDefinitions.push({ src: escapeRegex(route), dest });
}
}
await builder.buildISRFolder(entryFile, '_isr', isrConfig, _config.root);
for (const route of routes) {
const src = route.patternRegex.source;
const dest =
src.startsWith('^\\/_image') || src.startsWith('^\\/_server-islands')
? NODE_PATH
: ISR_PATH;
if (!route.isPrerendered) routeDefinitions.push({ src, dest });
// Do not create _isr route entries for excluded routes
const excludeRouteFromIsr = isrConfig.exclude?.some(exclusion => {
if (exclusion instanceof RegExp) {
return exclusion.test(route.pattern);
}

return exclusion === route.pattern;
});

if (!excludeRouteFromIsr) {
const src = route.patternRegex.source;
const dest =
src.startsWith('^\\/_image') || src.startsWith('^\\/_server-islands')
? NODE_PATH
: ISR_PATH;
if (!route.isPrerendered) routeDefinitions.push({ src, dest });
}
}
} else {
await builder.buildServerlessFolder(entryFile, NODE_PATH, _config.root);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default defineConfig({
isr: {
bypassToken: "1c9e601d-9943-4e7c-9575-005556d774a8",
expiration: 120,
exclude: ["/two", "/excluded/[dynamic]", "/excluded/[...rest]"]
exclude: ["/two", "/excluded/[dynamic]", "/excluded/[...rest]", /^\/api/]
}
})
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export async function GET({ params }) {
return new Response(`OK ${params.dynamic}`)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export async function GET() {
return new Response("OK")
}
22 changes: 9 additions & 13 deletions packages/integrations/vercel/test/isr.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,29 +42,25 @@ describe('ISR', () => {
dest: '_render',
},
{
src: '^/_server-islands/([^/]+?)/?$',
dest: '_render',
src: "^/api/([^/]+?)$",
dest: "_render"
},
{
src: '^/_image/?$',
dest: '_render',
src: "^/api$",
dest: "_render"
},
{
src: '^/excluded/([^/]+?)/?$',
dest: '/_isr?x_astro_path=$0',
src: '^/_server-islands/([^/]+?)/?$',
dest: '_render',
},
{
src: '^/excluded(?:/(.*?))?/?$',
dest: '/_isr?x_astro_path=$0',
src: '^/_image/?$',
dest: '_render',
},
{
src: '^/one/?$',
dest: '/_isr?x_astro_path=$0',
},
{
src: '^/two/?$',
dest: '/_isr?x_astro_path=$0',
},
}
]);
});
});

0 comments on commit 385b447

Please sign in to comment.