Skip to content

Commit 66b2f4e

Browse files
authored
fix: redirect when previewing prerendered pages (#9353)
closes #8839
1 parent 0f863bb commit 66b2f4e

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

.changeset/happy-panthers-leave.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
fix: redirect to path with/without trailing slash when previewing prerendered pages

packages/kit/src/exports/vite/preview/index.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,31 @@ export async function preview(vite, vite_config, svelte_config) {
108108
let prerendered = is_file(filename);
109109

110110
if (!prerendered) {
111-
filename += filename.endsWith('/') ? 'index.html' : '.html';
112-
prerendered = is_file(filename);
111+
const has_trailing_slash = pathname.endsWith('/');
112+
const html_filename = `${filename}${has_trailing_slash ? 'index.html' : '.html'}`;
113+
114+
let redirect;
115+
116+
if (is_file(html_filename)) {
117+
filename = html_filename;
118+
prerendered = true;
119+
} else if (has_trailing_slash) {
120+
if (is_file(filename.slice(0, -1) + '.html')) {
121+
redirect = pathname.slice(0, -1);
122+
}
123+
} else if (is_file(filename + '/index.html')) {
124+
redirect = pathname + '/';
125+
}
126+
127+
if (redirect) {
128+
res.writeHead(307, {
129+
location: redirect
130+
});
131+
132+
res.end();
133+
134+
return;
135+
}
113136
}
114137

115138
if (prerendered) {

0 commit comments

Comments
 (0)