Skip to content

Commit

Permalink
fix: add prerendered redirect for non-trailing slash routes with the …
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
eltigerchino authored Jan 30, 2023
1 parent ccf9659 commit b2575f8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/purple-starfishes-sip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/adapter-vercel': patch
---

fix: add trailing slash -> no trailing slash redirect for prerendered pages
22 changes: 15 additions & 7 deletions packages/adapter-vercel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,16 +193,24 @@ function static_vercel_config(builder) {
}

for (const [path, page] of builder.prerendered.pages) {
if (path.endsWith('/') && path !== '/') {
let overrides_path = path.slice(1);

if (path !== '/') {
/** @type {string | undefined} */
let counterpart_route = path + '/';

if (path.endsWith('/')) {
counterpart_route = path.slice(0, -1);
overrides_path = path.slice(1, -1);
}

prerendered_redirects.push(
{ src: path, dest: path.slice(0, -1) },
{ src: path.slice(0, -1), status: 308, headers: { Location: path } }
{ src: path, dest: counterpart_route },
{ src: counterpart_route, status: 308, headers: { Location: path } }
);

overrides[page.file] = { path: path.slice(1, -1) };
} else {
overrides[page.file] = { path: path.slice(1) };
}

overrides[page.file] = { path: overrides_path };
}

return {
Expand Down

0 comments on commit b2575f8

Please sign in to comment.