Skip to content

Commit

Permalink
Fix svelte-kit preview again (#4207)
Browse files Browse the repository at this point in the history
* unscope SSR handler

* override before creating server, doh

* i really screwed this up, huh

* Create quiet-knives-refuse.md
  • Loading branch information
Rich-Harris committed Mar 4, 2022
1 parent 28d10f9 commit bc6571e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/quiet-knives-refuse.md
@@ -0,0 +1,5 @@
---
"@sveltejs/kit": patch
---

Fix `svelte-kit preview`
13 changes: 7 additions & 6 deletions packages/kit/src/core/preview/index.js
Expand Up @@ -50,15 +50,15 @@ export async function preview({ port, host, config, https: use_https = false })
const { Server, override } = await import(pathToFileURL(index_file).href);
const { manifest } = await import(pathToFileURL(manifest_file).href);

const server = new Server(manifest);

override({
paths: { base, assets },
prerendering: false,
protocol: use_https ? 'https' : 'http',
read: (file) => fs.readFileSync(join(config.kit.files.assets, file))
});

const server = new Server(manifest);

const handle = compose([
// files in `static`
scoped(assets, mutable(config.kit.files.assets)),
Expand Down Expand Up @@ -96,7 +96,7 @@ export async function preview({ port, host, config, https: use_https = false })

if (normalized !== pathname) {
res.writeHead(307, {
location: normalized + search
location: base + normalized + search
});
res.end();
return;
Expand Down Expand Up @@ -124,7 +124,7 @@ export async function preview({ port, host, config, https: use_https = false })
}),

// SSR
scoped(base, async (req, res) => {
async (req, res) => {
const protocol = use_https ? 'https' : 'http';
const host = req.headers['host'];

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

setResponse(res, await server.respond(request));
})
}
]);

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

return (req, res, next) => {
if (req.url?.startsWith(scope)) {
const original_url = req.url;
req.url = req.url.slice(scope.length);
handler(req, res, () => {
req.url = scope + req.url;
req.url = original_url;
next();
});
} else {
Expand Down

0 comments on commit bc6571e

Please sign in to comment.