Skip to content

Commit b2575f8

Browse files
authored
fix: add prerendered redirect for non-trailing slash routes with the Vercel adapter (#8766)
fixes #8755 Adds a redirect for prerendered non-trailing slash routes to adapter-vercel. Previously, only prerendered trailing slash routes would be added to the redirects list in the vercel config file.
1 parent ccf9659 commit b2575f8

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

.changeset/purple-starfishes-sip.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/adapter-vercel': patch
3+
---
4+
5+
fix: add trailing slash -> no trailing slash redirect for prerendered pages

packages/adapter-vercel/index.js

+15-7
Original file line numberDiff line numberDiff line change
@@ -193,16 +193,24 @@ function static_vercel_config(builder) {
193193
}
194194

195195
for (const [path, page] of builder.prerendered.pages) {
196-
if (path.endsWith('/') && path !== '/') {
196+
let overrides_path = path.slice(1);
197+
198+
if (path !== '/') {
199+
/** @type {string | undefined} */
200+
let counterpart_route = path + '/';
201+
202+
if (path.endsWith('/')) {
203+
counterpart_route = path.slice(0, -1);
204+
overrides_path = path.slice(1, -1);
205+
}
206+
197207
prerendered_redirects.push(
198-
{ src: path, dest: path.slice(0, -1) },
199-
{ src: path.slice(0, -1), status: 308, headers: { Location: path } }
208+
{ src: path, dest: counterpart_route },
209+
{ src: counterpart_route, status: 308, headers: { Location: path } }
200210
);
201-
202-
overrides[page.file] = { path: path.slice(1, -1) };
203-
} else {
204-
overrides[page.file] = { path: path.slice(1) };
205211
}
212+
213+
overrides[page.file] = { path: overrides_path };
206214
}
207215

208216
return {

0 commit comments

Comments
 (0)