Skip to content

Commit 297edf8

Browse files
committed
fix: constructor correct pathname for isr from route with nested params
fixes #9459
1 parent c0612a8 commit 297edf8

File tree

6 files changed

+76
-9
lines changed

6 files changed

+76
-9
lines changed

.changeset/heavy-news-argue.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/adapter-vercel': patch
3+
---
4+
5+
fix: constructor correct pathname for isr from route with nested params

packages/adapter-vercel/index.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import path from 'node:path';
33
import { fileURLToPath } from 'node:url';
44
import { nodeFileTrace } from '@vercel/nft';
55
import esbuild from 'esbuild';
6+
import { get_pathname } from './utils.js';
67

78
const VALID_RUNTIMES = ['edge', 'nodejs16.x', 'nodejs18.x'];
89

@@ -292,13 +293,7 @@ const plugin = function (defaults = {}) {
292293
fs.symlinkSync(relative, `${base}.func`);
293294
fs.symlinkSync(`../${relative}`, `${base}/__data.json.func`);
294295

295-
let i = 1;
296-
const pathname = route.segments
297-
.map((segment) => {
298-
return segment.dynamic ? `$${i++}` : segment.content;
299-
})
300-
.join('/');
301-
296+
const pathname = get_pathname(route);
302297
const json = JSON.stringify(isr, null, '\t');
303298

304299
write(`${base}.prerender-config.json`, json);

packages/adapter-vercel/package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@
2020
"files": [
2121
"files",
2222
"index.js",
23+
"utils.js",
2324
"index.d.ts"
2425
],
2526
"scripts": {
2627
"lint": "prettier --check . --config ../../.prettierrc --ignore-path .gitignore",
2728
"format": "pnpm lint --write",
28-
"check": "tsc"
29+
"check": "tsc",
30+
"test": "uvu test .spec.js"
2931
},
3032
"dependencies": {
3133
"@vercel/nft": "^0.22.1",
@@ -34,7 +36,8 @@
3436
"devDependencies": {
3537
"@sveltejs/kit": "workspace:^",
3638
"@types/node": "^16.18.6",
37-
"typescript": "^4.9.4"
39+
"typescript": "^4.9.4",
40+
"uvu": "^0.5.6"
3841
},
3942
"peerDependencies": {
4043
"@sveltejs/kit": "^1.5.0"
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { test } from 'uvu';
2+
import * as assert from 'uvu/assert';
3+
import { get_pathname } from '../utils.js';
4+
5+
/**
6+
* @param {import('@sveltejs/kit').RouteDefinition<any>['segments']} segments
7+
* @param {string} expected
8+
*/
9+
function run_get_pathname_test(segments, expected) {
10+
const route = /** @type {import('@sveltejs/kit').RouteDefinition<any>} */ ({ segments });
11+
assert.equal(get_pathname(route), expected);
12+
}
13+
14+
test('get_pathname for simple route', () => {
15+
run_get_pathname_test([{ content: 'foo', dynamic: false, rest: false }], 'foo');
16+
});
17+
18+
test('get_pathname for route with parameters', () => {
19+
run_get_pathname_test(
20+
[
21+
{ content: 'foo', dynamic: false, rest: false },
22+
{ content: '[bar]', dynamic: true, rest: false }
23+
],
24+
'foo/$1'
25+
);
26+
});
27+
28+
test('get_pathname for route with parameters within segment', () => {
29+
run_get_pathname_test(
30+
[
31+
{ content: 'foo-[bar]', dynamic: true, rest: false },
32+
{ content: '[baz]-buz', dynamic: true, rest: false }
33+
],
34+
'foo-$1/$2-buz'
35+
);
36+
});
37+
38+
test.run();

packages/adapter-vercel/utils.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/** @param {import("@sveltejs/kit").RouteDefinition<any>} route */
2+
export function get_pathname(route) {
3+
let i = 1;
4+
5+
return route.segments
6+
.map((segment) => {
7+
console.log(segment);
8+
if (!segment.dynamic) {
9+
return segment.content;
10+
}
11+
12+
const parts = segment.content.split(/\[(.+?)\](?!\])/);
13+
return parts
14+
.map((content, j) => {
15+
if (j % 2) {
16+
return `$${i++}`;
17+
} else {
18+
return content;
19+
}
20+
})
21+
.join('');
22+
})
23+
.join('/');
24+
}

pnpm-lock.yaml

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)