Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix svelte-kit preview again #4207

Merged
merged 4 commits into from
Mar 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/quiet-knives-refuse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@sveltejs/kit": patch
---

Fix `svelte-kit preview`
13 changes: 7 additions & 6 deletions packages/kit/src/core/preview/index.js
Original file line number Diff line number Diff line change
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