Skip to content

Commit

Permalink
Fix preview when base path is set
Browse files Browse the repository at this point in the history
  • Loading branch information
Karlinator committed Sep 20, 2021
1 parent 3f4237e commit 452eed4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/good-monkeys-reflect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Fix preview when `kit.paths.base` is set.
24 changes: 12 additions & 12 deletions packages/kit/src/core/preview/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,15 @@ export async function preview({

const parsed = new URL(initial_url, 'http://localhost/');

const rendered =
parsed.pathname.startsWith(config.kit.paths.base) &&
(await app.render({
host: /** @type {string} */ (
config.kit.host || req.headers[config.kit.hostHeader || 'host']
),
method: req.method,
headers: /** @type {import('types/helper').RequestHeaders} */ (req.headers),
path: parsed.pathname.replace(config.kit.paths.base, ''),
query: parsed.searchParams,
rawBody: body
}));
const rendered = await app.render({
host: /** @type {string} */ (config.kit.host ||
req.headers[config.kit.hostHeader || 'host']),
method: req.method,
headers: /** @type {import('types/helper').RequestHeaders} */ (req.headers),
path: parsed.pathname.replace(config.kit.paths.base, ''),
query: parsed.searchParams,
rawBody: body
});

if (rendered) {
res.writeHead(rendered.status, rendered.headers);
Expand All @@ -120,6 +117,9 @@ export async function preview({
render_handler();
}
} else {
if (initial_url.startsWith(config.kit.paths.base)) {
req.url = initial_url.slice(config.kit.paths.base.length);
}
assets_handler(req, res, () => {
static_handler(req, res, render_handler);
});
Expand Down

0 comments on commit 452eed4

Please sign in to comment.