Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: construct correct pathname for isr from route with nested params #9470

Merged
merged 5 commits into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/heavy-news-argue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/adapter-vercel': patch
---

fix: constructor correct pathname for isr from route with nested params
4 changes: 3 additions & 1 deletion packages/adapter-vercel/files/serverless.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ export default async (req, res) => {
const [path, search] = req.url.split('?');

const params = new URLSearchParams(search);
const pathname = params.get('__pathname');
let pathname = params.get('__pathname');

if (pathname) {
params.delete('__pathname');
// Optional routes' pathname replacements look like `/foo/$1/bar` which means we could end up with an url like /foo//bar
pathname = pathname.replace(/\/+/g, '/');
req.url = `${pathname}${path.endsWith(DATA_SUFFIX) ? DATA_SUFFIX : ''}?${params}`;
dummdidumm marked this conversation as resolved.
Show resolved Hide resolved
}
}
Expand Down
9 changes: 2 additions & 7 deletions packages/adapter-vercel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { nodeFileTrace } from '@vercel/nft';
import esbuild from 'esbuild';
import { get_pathname } from './utils.js';

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

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

let i = 1;
const pathname = route.segments
.map((segment) => {
return segment.dynamic ? `$${i++}` : segment.content;
})
.join('/');

const pathname = get_pathname(route);
const json = JSON.stringify(isr, null, '\t');

write(`${base}.prerender-config.json`, json);
Expand Down
7 changes: 5 additions & 2 deletions packages/adapter-vercel/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
"files": [
"files",
"index.js",
"utils.js",
"index.d.ts"
],
"scripts": {
"lint": "prettier --check . --config ../../.prettierrc --ignore-path .gitignore",
"format": "pnpm lint --write",
"check": "tsc"
"check": "tsc",
"test": "uvu test .spec.js"
},
"dependencies": {
"@vercel/nft": "^0.22.1",
Expand All @@ -34,7 +36,8 @@
"devDependencies": {
"@sveltejs/kit": "workspace:^",
"@types/node": "^16.18.6",
"typescript": "^4.9.4"
"typescript": "^4.9.4",
"uvu": "^0.5.6"
},
"peerDependencies": {
"@sveltejs/kit": "^1.5.0"
Expand Down
38 changes: 38 additions & 0 deletions packages/adapter-vercel/test/utils.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { test } from 'uvu';
import * as assert from 'uvu/assert';
import { get_pathname } from '../utils.js';

/**
* @param {import('@sveltejs/kit').RouteDefinition<any>['segments']} segments
* @param {string} expected
*/
function run_get_pathname_test(segments, expected) {
const route = /** @type {import('@sveltejs/kit').RouteDefinition<any>} */ ({ segments });
assert.equal(get_pathname(route), expected);
}

test('get_pathname for simple route', () => {
run_get_pathname_test([{ content: 'foo', dynamic: false, rest: false }], 'foo');
});

test('get_pathname for route with parameters', () => {
run_get_pathname_test(
[
{ content: 'foo', dynamic: false, rest: false },
{ content: '[bar]', dynamic: true, rest: false }
],
'foo/$1'
);
});

test('get_pathname for route with parameters within segment', () => {
run_get_pathname_test(
[
{ content: 'foo-[bar]', dynamic: true, rest: false },
{ content: '[baz]-buz', dynamic: true, rest: false }
],
'foo-$1/$2-buz'
);
});

test.run();
24 changes: 24 additions & 0 deletions packages/adapter-vercel/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/** @param {import("@sveltejs/kit").RouteDefinition<any>} route */
export function get_pathname(route) {
let i = 1;

return route.segments
.map((segment) => {
console.log(segment);
if (!segment.dynamic) {
return segment.content;
}

const parts = segment.content.split(/\[(.+?)\](?!\])/);
return parts
.map((content, j) => {
if (j % 2) {
return `$${i++}`;
} else {
return content;
}
})
.join('');
})
.join('/');
}
2 changes: 2 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.