Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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/large-pugs-clap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/adapter-static': patch
---

fix: match adapter-vercel's prerendered redirect handling when deploying to Vercel
34 changes: 26 additions & 8 deletions packages/adapter-static/platforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,17 @@ function static_vercel_config(builder) {
/** @type {Record<string, { path: string }>} */
const overrides = {};

for (const [src, redirect] of builder.prerendered.redirects) {
for (let [src, redirect] of builder.prerendered.redirects) {
if (src.replace(/\/$/, '') === redirect.location.replace(/\/$/, '')) {
// ignore the extreme edge case of a `/foo` -> `/foo/` redirect,
// which would only arise if the response was generated by a
// `handle` hook or outside the app altogether (since you
// can't declaratively create both routes)
} else {
// redirect both `/foo` and `/foo/` to `redirect.location`
src = src.replace(/\/?$/, '/?');
}

prerendered_redirects.push({
src,
headers: {
Expand All @@ -30,16 +40,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
Loading