From bc6571e0a98639c924d6f221e24c14759470b398 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Thu, 3 Mar 2022 22:28:25 -0500 Subject: [PATCH] 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 --- .changeset/quiet-knives-refuse.md | 5 +++++ packages/kit/src/core/preview/index.js | 13 +++++++------ 2 files changed, 12 insertions(+), 6 deletions(-) create mode 100644 .changeset/quiet-knives-refuse.md diff --git a/.changeset/quiet-knives-refuse.md b/.changeset/quiet-knives-refuse.md new file mode 100644 index 000000000000..020d1a687959 --- /dev/null +++ b/.changeset/quiet-knives-refuse.md @@ -0,0 +1,5 @@ +--- +"@sveltejs/kit": patch +--- + +Fix `svelte-kit preview` diff --git a/packages/kit/src/core/preview/index.js b/packages/kit/src/core/preview/index.js index 57920864706c..1340358b8705 100644 --- a/packages/kit/src/core/preview/index.js +++ b/packages/kit/src/core/preview/index.js @@ -50,8 +50,6 @@ 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, @@ -59,6 +57,8 @@ export async function preview({ port, host, config, https: use_https = false }) 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)), @@ -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; @@ -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']; @@ -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())) || {}; @@ -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 {