From ad99e9eb845b8874b806b617a2fd0823245acaee Mon Sep 17 00:00:00 2001 From: Karlinator Date: Sun, 12 Sep 2021 05:54:51 +0200 Subject: [PATCH 1/2] Fix preview when base path is set --- .changeset/good-monkeys-reflect.md | 5 +++++ packages/kit/src/core/preview/index.js | 25 +++++++++++++------------ 2 files changed, 18 insertions(+), 12 deletions(-) create mode 100644 .changeset/good-monkeys-reflect.md diff --git a/.changeset/good-monkeys-reflect.md b/.changeset/good-monkeys-reflect.md new file mode 100644 index 000000000000..6e9c4dc0b34f --- /dev/null +++ b/.changeset/good-monkeys-reflect.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +Fix preview when `kit.paths.base` is set. diff --git a/packages/kit/src/core/preview/index.js b/packages/kit/src/core/preview/index.js index 28be0d816866..9936349d6531 100644 --- a/packages/kit/src/core/preview/index.js +++ b/packages/kit/src/core/preview/index.js @@ -86,18 +86,16 @@ 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); @@ -120,6 +118,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); }); From 19c170bac89db17e269dc1ca6e936ebf11873587 Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Wed, 22 Sep 2021 08:35:11 -0700 Subject: [PATCH 2/2] Add back check to see if path starts with base url --- packages/kit/src/core/preview/index.js | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/packages/kit/src/core/preview/index.js b/packages/kit/src/core/preview/index.js index 9936349d6531..c4d7a62bd034 100644 --- a/packages/kit/src/core/preview/index.js +++ b/packages/kit/src/core/preview/index.js @@ -86,16 +86,18 @@ export async function preview({ const parsed = new URL(initial_url, 'http://localhost/'); - 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 - }); + 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 + })); if (rendered) { res.writeHead(rendered.status, rendered.headers);