From 7406d07fbee78e436e9b3f756fbb10422e9cab91 Mon Sep 17 00:00:00 2001 From: Ivan Starkov Date: Mon, 9 Aug 2021 18:55:29 +0200 Subject: [PATCH 1/8] Add broken test --- .../test/apps/options-paths-base/package.json | 16 ++++++++++ .../source/pages/base/_tests.js | 9 ++++++ .../source/pages/base/index.svelte | 23 +++++++++++++++ .../options-paths-base/source/template.html | 12 ++++++++ .../apps/options-paths-base/svelte.config.js | 29 +++++++++++++++++++ 5 files changed, 89 insertions(+) create mode 100644 packages/kit/test/apps/options-paths-base/package.json create mode 100644 packages/kit/test/apps/options-paths-base/source/pages/base/_tests.js create mode 100644 packages/kit/test/apps/options-paths-base/source/pages/base/index.svelte create mode 100644 packages/kit/test/apps/options-paths-base/source/template.html create mode 100644 packages/kit/test/apps/options-paths-base/svelte.config.js diff --git a/packages/kit/test/apps/options-paths-base/package.json b/packages/kit/test/apps/options-paths-base/package.json new file mode 100644 index 000000000000..1cbb024f2f09 --- /dev/null +++ b/packages/kit/test/apps/options-paths-base/package.json @@ -0,0 +1,16 @@ +{ + "name": "test-options", + "private": true, + "version": "0.0.1", + "scripts": { + "dev": "../../../svelte-kit.js dev", + "build": "../../../svelte-kit.js build", + "preview": "../../../svelte-kit.js preview" + }, + "devDependencies": { + "@sveltejs/adapter-node": "workspace:*", + "@sveltejs/kit": "workspace:*", + "svelte": "^3.40.0" + }, + "type": "module" +} diff --git a/packages/kit/test/apps/options-paths-base/source/pages/base/_tests.js b/packages/kit/test/apps/options-paths-base/source/pages/base/_tests.js new file mode 100644 index 000000000000..b5859b6efb32 --- /dev/null +++ b/packages/kit/test/apps/options-paths-base/source/pages/base/_tests.js @@ -0,0 +1,9 @@ +import * as assert from 'uvu/assert'; + +/** @type {import('test').TestMaker} */ +export default function (test) { + test('sets_paths', '/base/', async ({ page }) => { + assert.equal(await page.textContent('[data-source="base"]'), '/path-base'); + assert.equal(await page.textContent('[data-source="assets"]'), '/path-base'); + }); +} diff --git a/packages/kit/test/apps/options-paths-base/source/pages/base/index.svelte b/packages/kit/test/apps/options-paths-base/source/pages/base/index.svelte new file mode 100644 index 000000000000..62ea289c7c64 --- /dev/null +++ b/packages/kit/test/apps/options-paths-base/source/pages/base/index.svelte @@ -0,0 +1,23 @@ + + + + +

{data.base}

+

{data.assets}

diff --git a/packages/kit/test/apps/options-paths-base/source/template.html b/packages/kit/test/apps/options-paths-base/source/template.html new file mode 100644 index 000000000000..20093c610559 --- /dev/null +++ b/packages/kit/test/apps/options-paths-base/source/template.html @@ -0,0 +1,12 @@ + + + + + + %svelte.head% + + +

I am in the template

+
%svelte.body%
+ + diff --git a/packages/kit/test/apps/options-paths-base/svelte.config.js b/packages/kit/test/apps/options-paths-base/svelte.config.js new file mode 100644 index 000000000000..082e4b0adc3a --- /dev/null +++ b/packages/kit/test/apps/options-paths-base/svelte.config.js @@ -0,0 +1,29 @@ +/** @type {import('@sveltejs/kit').Config} */ +const config = { + extensions: ['.jesuslivesineveryone', '.whokilledthemuffinman', '.svelte.md', '.svelte'], + kit: { + files: { + assets: 'public', + lib: 'source/components', + routes: 'source/pages', + template: 'source/template.html' + }, + // appDir: '_wheee', + floc: true, + target: '#content-goes-here', + host: 'example.com', + trailingSlash: 'always', + vite: { + build: { + minify: false + }, + clearScreen: false + }, + paths: { + base: '/path-base', + assets: '' + } + } +}; + +export default config; From 4b44278d5343a76d8facffd30a7fba9b4aeb7964 Mon Sep 17 00:00:00 2001 From: Ivan Starkov Date: Mon, 9 Aug 2021 20:55:23 +0200 Subject: [PATCH 2/8] Fix dev and preview --- packages/kit/src/core/dev/index.js | 6 ++++++ packages/kit/src/core/preview/index.js | 16 +++++++++++----- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/packages/kit/src/core/dev/index.js b/packages/kit/src/core/dev/index.js index b6be54f0bead..8ddc5a7f4b47 100644 --- a/packages/kit/src/core/dev/index.js +++ b/packages/kit/src/core/dev/index.js @@ -306,6 +306,12 @@ async function create_handler(vite, config, dir, cwd, manifest) { const root = (await vite.ssrLoadModule(`/${dir}/generated/root.svelte`)).default; + const paths = await vite.ssrLoadModule(`/${SVELTE_KIT}/dev/runtime/paths.js`); + + if (paths != null && config.kit.paths != null) { + paths.set_paths(config.kit.paths); + } + let body; try { diff --git a/packages/kit/src/core/preview/index.js b/packages/kit/src/core/preview/index.js index cf965ab938cd..ccef55833e3b 100644 --- a/packages/kit/src/core/preview/index.js +++ b/packages/kit/src/core/preview/index.js @@ -51,10 +51,7 @@ export async function preview({ }); app.init({ - paths: { - base: '', - assets: '/.' - }, + paths: config.kit.paths, prerendering: false, read: (file) => fs.readFileSync(join(config.kit.files.assets, file)) }); @@ -63,7 +60,14 @@ export async function preview({ const vite_config = (config.kit.vite && config.kit.vite()) || {}; const server = await get_server(use_https, vite_config, (req, res) => { - const parsed = parse(req.url || ''); + const initial_url = req.url; + const { assets } = config.kit.paths; + + // Emulate app.use(`${assets}/`, sirv(...)) + req.url = + assets.length > 1 && assets !== '/.' && req.url?.startsWith(`${assets}/`) + ? req.url.slice(assets.length) + : req.url; assets_handler(req, res, () => { static_handler(req, res, async () => { @@ -78,6 +82,8 @@ export async function preview({ return res.end(err.reason || 'Invalid request body'); } + const parsed = parse(initial_url || ''); + const rendered = await app.render({ host: /** @type {string} */ (config.kit.host || req.headers[config.kit.hostHeader || 'host']), From 8a370a2f0eca6b89d5b96ce01b0a7c24c5538913 Mon Sep 17 00:00:00 2001 From: Ivan Starkov Date: Mon, 9 Aug 2021 21:55:51 +0200 Subject: [PATCH 3/8] Try without ?. --- packages/kit/src/core/preview/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/kit/src/core/preview/index.js b/packages/kit/src/core/preview/index.js index ccef55833e3b..50b5bf1401da 100644 --- a/packages/kit/src/core/preview/index.js +++ b/packages/kit/src/core/preview/index.js @@ -65,7 +65,7 @@ export async function preview({ // Emulate app.use(`${assets}/`, sirv(...)) req.url = - assets.length > 1 && assets !== '/.' && req.url?.startsWith(`${assets}/`) + assets.length > 1 && assets !== '/.' && req.url.startsWith(`${assets}/`) ? req.url.slice(assets.length) : req.url; From 2df57148dcbb7fd5e82a3b089e5ae58bd450f142 Mon Sep 17 00:00:00 2001 From: Ivan Starkov Date: Mon, 9 Aug 2021 22:18:47 +0200 Subject: [PATCH 4/8] Move tests to options --- packages/kit/src/core/dev/index.js | 4 +-- packages/kit/src/core/preview/index.js | 4 +++ .../test/apps/options-paths-base/package.json | 16 ---------- .../options-paths-base/source/template.html | 12 -------- .../apps/options-paths-base/svelte.config.js | 29 ------------------- .../source/pages/base/_tests.js | 0 .../source/pages/base/index.svelte | 0 .../options/source/pages/host/index.svelte | 4 ++- .../kit/test/apps/options/svelte.config.js | 11 ++++++- 9 files changed, 18 insertions(+), 62 deletions(-) delete mode 100644 packages/kit/test/apps/options-paths-base/package.json delete mode 100644 packages/kit/test/apps/options-paths-base/source/template.html delete mode 100644 packages/kit/test/apps/options-paths-base/svelte.config.js rename packages/kit/test/apps/{options-paths-base => options}/source/pages/base/_tests.js (100%) rename packages/kit/test/apps/{options-paths-base => options}/source/pages/base/index.svelte (100%) diff --git a/packages/kit/src/core/dev/index.js b/packages/kit/src/core/dev/index.js index 8ddc5a7f4b47..1545bd01d583 100644 --- a/packages/kit/src/core/dev/index.js +++ b/packages/kit/src/core/dev/index.js @@ -308,9 +308,7 @@ async function create_handler(vite, config, dir, cwd, manifest) { const paths = await vite.ssrLoadModule(`/${SVELTE_KIT}/dev/runtime/paths.js`); - if (paths != null && config.kit.paths != null) { - paths.set_paths(config.kit.paths); - } + paths.set_paths(config.kit.paths); let body; diff --git a/packages/kit/src/core/preview/index.js b/packages/kit/src/core/preview/index.js index 50b5bf1401da..6bcc5bf25528 100644 --- a/packages/kit/src/core/preview/index.js +++ b/packages/kit/src/core/preview/index.js @@ -60,6 +60,10 @@ export async function preview({ const vite_config = (config.kit.vite && config.kit.vite()) || {}; const server = await get_server(use_https, vite_config, (req, res) => { + if (req.url == null) { + throw new Error('Invalid request url'); + } + const initial_url = req.url; const { assets } = config.kit.paths; diff --git a/packages/kit/test/apps/options-paths-base/package.json b/packages/kit/test/apps/options-paths-base/package.json deleted file mode 100644 index 1cbb024f2f09..000000000000 --- a/packages/kit/test/apps/options-paths-base/package.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "name": "test-options", - "private": true, - "version": "0.0.1", - "scripts": { - "dev": "../../../svelte-kit.js dev", - "build": "../../../svelte-kit.js build", - "preview": "../../../svelte-kit.js preview" - }, - "devDependencies": { - "@sveltejs/adapter-node": "workspace:*", - "@sveltejs/kit": "workspace:*", - "svelte": "^3.40.0" - }, - "type": "module" -} diff --git a/packages/kit/test/apps/options-paths-base/source/template.html b/packages/kit/test/apps/options-paths-base/source/template.html deleted file mode 100644 index 20093c610559..000000000000 --- a/packages/kit/test/apps/options-paths-base/source/template.html +++ /dev/null @@ -1,12 +0,0 @@ - - - - - - %svelte.head% - - -

I am in the template

-
%svelte.body%
- - diff --git a/packages/kit/test/apps/options-paths-base/svelte.config.js b/packages/kit/test/apps/options-paths-base/svelte.config.js deleted file mode 100644 index 082e4b0adc3a..000000000000 --- a/packages/kit/test/apps/options-paths-base/svelte.config.js +++ /dev/null @@ -1,29 +0,0 @@ -/** @type {import('@sveltejs/kit').Config} */ -const config = { - extensions: ['.jesuslivesineveryone', '.whokilledthemuffinman', '.svelte.md', '.svelte'], - kit: { - files: { - assets: 'public', - lib: 'source/components', - routes: 'source/pages', - template: 'source/template.html' - }, - // appDir: '_wheee', - floc: true, - target: '#content-goes-here', - host: 'example.com', - trailingSlash: 'always', - vite: { - build: { - minify: false - }, - clearScreen: false - }, - paths: { - base: '/path-base', - assets: '' - } - } -}; - -export default config; diff --git a/packages/kit/test/apps/options-paths-base/source/pages/base/_tests.js b/packages/kit/test/apps/options/source/pages/base/_tests.js similarity index 100% rename from packages/kit/test/apps/options-paths-base/source/pages/base/_tests.js rename to packages/kit/test/apps/options/source/pages/base/_tests.js diff --git a/packages/kit/test/apps/options-paths-base/source/pages/base/index.svelte b/packages/kit/test/apps/options/source/pages/base/index.svelte similarity index 100% rename from packages/kit/test/apps/options-paths-base/source/pages/base/index.svelte rename to packages/kit/test/apps/options/source/pages/base/index.svelte diff --git a/packages/kit/test/apps/options/source/pages/host/index.svelte b/packages/kit/test/apps/options/source/pages/host/index.svelte index b1078fb1e0c5..341a9091c2b8 100644 --- a/packages/kit/test/apps/options/source/pages/host/index.svelte +++ b/packages/kit/test/apps/options/source/pages/host/index.svelte @@ -1,7 +1,9 @@