Skip to content

Commit bc6571e

Browse files
authored
Fix svelte-kit preview again (#4207)
* unscope SSR handler * override before creating server, doh * i really screwed this up, huh * Create quiet-knives-refuse.md
1 parent 28d10f9 commit bc6571e

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

.changeset/quiet-knives-refuse.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 `svelte-kit preview`

packages/kit/src/core/preview/index.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,15 @@ export async function preview({ port, host, config, https: use_https = false })
5050
const { Server, override } = await import(pathToFileURL(index_file).href);
5151
const { manifest } = await import(pathToFileURL(manifest_file).href);
5252

53-
const server = new Server(manifest);
54-
5553
override({
5654
paths: { base, assets },
5755
prerendering: false,
5856
protocol: use_https ? 'https' : 'http',
5957
read: (file) => fs.readFileSync(join(config.kit.files.assets, file))
6058
});
6159

60+
const server = new Server(manifest);
61+
6262
const handle = compose([
6363
// files in `static`
6464
scoped(assets, mutable(config.kit.files.assets)),
@@ -96,7 +96,7 @@ export async function preview({ port, host, config, https: use_https = false })
9696

9797
if (normalized !== pathname) {
9898
res.writeHead(307, {
99-
location: normalized + search
99+
location: base + normalized + search
100100
});
101101
res.end();
102102
return;
@@ -124,7 +124,7 @@ export async function preview({ port, host, config, https: use_https = false })
124124
}),
125125

126126
// SSR
127-
scoped(base, async (req, res) => {
127+
async (req, res) => {
128128
const protocol = use_https ? 'https' : 'http';
129129
const host = req.headers['host'];
130130

@@ -138,7 +138,7 @@ export async function preview({ port, host, config, https: use_https = false })
138138
}
139139

140140
setResponse(res, await server.respond(request));
141-
})
141+
}
142142
]);
143143

144144
const vite_config = (config.kit.vite && (await config.kit.vite())) || {};
@@ -219,9 +219,10 @@ function scoped(scope, handler) {
219219

220220
return (req, res, next) => {
221221
if (req.url?.startsWith(scope)) {
222+
const original_url = req.url;
222223
req.url = req.url.slice(scope.length);
223224
handler(req, res, () => {
224-
req.url = scope + req.url;
225+
req.url = original_url;
225226
next();
226227
});
227228
} else {

0 commit comments

Comments
 (0)