Skip to content
This repository was archived by the owner on Feb 10, 2025. It is now read-only.

Commit 5b802a4

Browse files
hrabielbholmesdev
andauthored
fix: ISR opt out for routes with rest parameters (#373)
Co-authored-by: Ben Holmes <hey@bholmes.dev>
1 parent 1b18e67 commit 5b802a4

File tree

5 files changed

+45
-18
lines changed

5 files changed

+45
-18
lines changed

.changeset/early-scissors-wait.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@astrojs/vercel': patch
3+
---
4+
5+
Fix excluding routes with rest parameters from ISR

packages/vercel/src/lib/redirects.ts

+23-17
Original file line numberDiff line numberDiff line change
@@ -45,25 +45,31 @@ function getParts(part: string, file: string) {
4545
// 2022-04-26
4646
function getMatchPattern(segments: RoutePart[][]) {
4747
return segments
48-
.map((segment) => {
49-
return segment[0].spread
48+
.map((segment, segmentIndex) => {
49+
return segment.length === 1 && segment[0].spread
5050
? '(?:\\/(.*?))?'
51-
: segment
52-
.map((part) => {
53-
if (part)
54-
return part.dynamic
55-
? '([^/]+?)'
56-
: part.content
57-
.normalize()
58-
.replace(/\?/g, '%3F')
59-
.replace(/#/g, '%23')
60-
.replace(/%5B/g, '[')
61-
.replace(/%5D/g, ']')
62-
.replace(/[*+?^${}()|[\]\\]/g, '\\$&');
63-
})
64-
.join('');
51+
: // Omit leading slash if segment is a spread.
52+
// This is handled using a regex in Astro core.
53+
// To avoid complex data massaging, we handle in-place here.
54+
(segmentIndex === 0 ? '' : '/') +
55+
segment
56+
.map((part) => {
57+
if (part)
58+
return part.spread
59+
? '(.*?)'
60+
: part.dynamic
61+
? '([^/]+?)'
62+
: part.content
63+
.normalize()
64+
.replace(/\?/g, '%3F')
65+
.replace(/#/g, '%23')
66+
.replace(/%5B/g, '[')
67+
.replace(/%5D/g, ']')
68+
.replace(/[*+?^${}()|[\]\\]/g, '\\$&');
69+
})
70+
.join('');
6571
})
66-
.join('/');
72+
.join('');
6773
}
6874

6975
function getReplacePattern(segments: RoutePart[][]) {

packages/vercel/test/fixtures/isr/astro.config.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export default defineConfig({
77
isr: {
88
bypassToken: "1c9e601d-9943-4e7c-9575-005556d774a8",
99
expiration: 120,
10-
exclude: ["/two", "/excluded/[dynamic]"]
10+
exclude: ["/two", "/excluded/[dynamic]", "/excluded/[...rest]"]
1111
}
1212
})
1313
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<html>
2+
<head>
3+
<title>Rest</title>
4+
</head>
5+
<body>
6+
<h1>Rest</h1>
7+
</body>
8+
</html>

packages/vercel/test/isr.test.js

+8
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ describe('ISR', () => {
3737
src: '^/excluded/([^/]+?)$',
3838
dest: '_render',
3939
},
40+
{
41+
src: '^/excluded(?:\\/(.*?))?$',
42+
dest: '_render',
43+
},
4044
{
4145
src: '^\\/_image$',
4246
dest: '_render',
@@ -45,6 +49,10 @@ describe('ISR', () => {
4549
src: '^\\/excluded\\/([^/]+?)\\/?$',
4650
dest: '/_isr?x_astro_path=$0',
4751
},
52+
{
53+
src: '^\\/excluded(?:\\/(.*?))?\\/?$',
54+
dest: '/_isr?x_astro_path=$0',
55+
},
4856
{
4957
src: '^\\/one\\/?$',
5058
dest: '/_isr?x_astro_path=$0',

0 commit comments

Comments
 (0)