Skip to content

Commit

Permalink
fix: redirect when previewing prerendered pages (#9353)
Browse files Browse the repository at this point in the history
closes #8839
  • Loading branch information
Rich-Harris authored Mar 8, 2023
1 parent 0f863bb commit 66b2f4e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/happy-panthers-leave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: redirect to path with/without trailing slash when previewing prerendered pages
27 changes: 25 additions & 2 deletions packages/kit/src/exports/vite/preview/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,31 @@ export async function preview(vite, vite_config, svelte_config) {
let prerendered = is_file(filename);

if (!prerendered) {
filename += filename.endsWith('/') ? 'index.html' : '.html';
prerendered = is_file(filename);
const has_trailing_slash = pathname.endsWith('/');
const html_filename = `${filename}${has_trailing_slash ? 'index.html' : '.html'}`;

let redirect;

if (is_file(html_filename)) {
filename = html_filename;
prerendered = true;
} else if (has_trailing_slash) {
if (is_file(filename.slice(0, -1) + '.html')) {
redirect = pathname.slice(0, -1);
}
} else if (is_file(filename + '/index.html')) {
redirect = pathname + '/';
}

if (redirect) {
res.writeHead(307, {
location: redirect
});

res.end();

return;
}
}

if (prerendered) {
Expand Down

0 comments on commit 66b2f4e

Please sign in to comment.