From 6e0d0bde6c716b3cbde5b715d16f5af7eaf8a650 Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Mon, 18 Jul 2022 13:19:27 -0700 Subject: [PATCH 01/31] set Vite's publicDir option and change Vite's output dir --- .changeset/four-pandas-push.md | 5 +++ .../kit/src/core/generate_manifest/index.js | 1 - packages/kit/src/vite/build/build_server.js | 2 +- packages/kit/src/vite/build/utils.js | 12 +++--- packages/kit/src/vite/dev/index.js | 42 ------------------- packages/kit/src/vite/index.js | 7 ++-- packages/kit/src/vite/preview/index.js | 3 -- 7 files changed, 15 insertions(+), 57 deletions(-) create mode 100644 .changeset/four-pandas-push.md diff --git a/.changeset/four-pandas-push.md b/.changeset/four-pandas-push.md new file mode 100644 index 000000000000..52d7c5e37aca --- /dev/null +++ b/.changeset/four-pandas-push.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +set Vite's publicDir option and change Vite's output dir diff --git a/packages/kit/src/core/generate_manifest/index.js b/packages/kit/src/core/generate_manifest/index.js index 983c8501e99c..ac61b4f21269 100644 --- a/packages/kit/src/core/generate_manifest/index.js +++ b/packages/kit/src/core/generate_manifest/index.js @@ -64,7 +64,6 @@ export function generate_manifest({ build_data, relative_path, routes, format = // prettier-ignore return `{ - appDir: ${s(build_data.app_dir)}, assets: new Set(${s(assets)}), mimeTypes: ${s(get_mime_lookup(build_data.manifest_data))}, _: { diff --git a/packages/kit/src/vite/build/build_server.js b/packages/kit/src/vite/build/build_server.js index 63be2f5b1bc3..38e006d64c84 100644 --- a/packages/kit/src/vite/build/build_server.js +++ b/packages/kit/src/vite/build/build_server.js @@ -73,7 +73,7 @@ export class Server { manifest, method_override: ${s(config.kit.methodOverride)}, paths: { base, assets }, - prefix: assets + '/${config.kit.appDir}/', + prefix: '/' + assets, prerender: { default: ${config.kit.prerender.default}, enabled: ${config.kit.prerender.enabled} diff --git a/packages/kit/src/vite/build/utils.js b/packages/kit/src/vite/build/utils.js index 916f8d736e7e..1beb7fca7317 100644 --- a/packages/kit/src/vite/build/utils.js +++ b/packages/kit/src/vite/build/utils.js @@ -96,18 +96,16 @@ export const get_default_config = function ({ config, input, ssr, outDir }) { input, output: { format: 'esm', - entryFileNames: ssr ? '[name].js' : 'immutable/[name]-[hash].js', - chunkFileNames: 'immutable/chunks/[name]-[hash].js', - assetFileNames: 'immutable/assets/[name]-[hash][extname]' + entryFileNames: ssr ? '[name].js' : `${config.kit.appDir}/immutable/[name]-[hash].js`, + chunkFileNames: `${config.kit.appDir}/immutable/chunks/[name]-[hash].js`, + assetFileNames: `${config.kit.appDir}/immutable/assets/[name]-[hash][extname]` }, preserveEntrySignatures: 'strict' }, ssr, target: ssr ? 'node14.8' : undefined }, - // prevent Vite copying the contents of `config.kit.files.assets`, - // if it happens to be 'public' instead of 'static' - publicDir: false, + publicDir: ssr ? false : config.kit.files.assets, resolve: { alias: get_aliases(config.kit) }, @@ -134,7 +132,7 @@ export function assets_base(config) { // during `svelte-kit preview`, because we use a local asset path. This // may be fixed in Vite 3: https://github.com/vitejs/vite/issues/2009 const { base, assets } = config.paths; - return `${assets || base}/${config.appDir}/`; + return `${assets || base}/`; } /** diff --git a/packages/kit/src/vite/dev/index.js b/packages/kit/src/vite/dev/index.js index fb2f30580ac8..29395d4a08d2 100644 --- a/packages/kit/src/vite/dev/index.js +++ b/packages/kit/src/vite/dev/index.js @@ -1,7 +1,6 @@ import fs from 'fs'; import colors from 'kleur'; import path from 'path'; -import sirv from 'sirv'; import { URL } from 'url'; import { getRequest, setResponse } from '../../node/index.js'; import { installPolyfills } from '../../node/polyfills.js'; @@ -175,12 +174,6 @@ export async function dev(vite, vite_config, svelte_config) { } const assets = svelte_config.kit.paths.assets ? SVELTE_KIT_ASSETS : svelte_config.kit.paths.base; - const asset_server = sirv(svelte_config.kit.files.assets, { - dev: true, - etag: true, - maxAge: 0, - extensions: [] - }); return () => { const serve_static_middleware = vite.middlewares.stack.find( @@ -199,20 +192,6 @@ export async function dev(vite, vite_config, svelte_config) { }`; const decoded = decodeURI(new URL(base + req.url).pathname); - - if (decoded.startsWith(assets)) { - const pathname = decoded.slice(assets.length); - const file = svelte_config.kit.files.assets + pathname; - - if (fs.existsSync(file) && !fs.statSync(file).isDirectory()) { - if (has_correct_case(file, svelte_config.kit.files.assets)) { - req.url = encodeURI(pathname); // don't need query/hash - asset_server(req, res); - return; - } - } - } - const file = posixify(path.resolve(decoded.slice(1))); const is_file = fs.existsSync(file) && !fs.statSync(file).isDirectory(); const allowed = @@ -446,24 +425,3 @@ async function find_deps(vite, node, deps) { await Promise.all(branches); } - -/** - * Determine if a file is being requested with the correct case, - * to ensure consistent behaviour between dev and prod and across - * operating systems. Note that we can't use realpath here, - * because we don't want to follow symlinks - * @param {string} file - * @param {string} assets - * @returns {boolean} - */ -function has_correct_case(file, assets) { - if (file === assets) return true; - - const parent = path.dirname(file); - - if (fs.readdirSync(parent).includes(path.basename(file))) { - return has_correct_case(parent, assets); - } - - return false; -} diff --git a/packages/kit/src/vite/index.js b/packages/kit/src/vite/index.js index 58e746e1e72f..b89a40de1ea3 100644 --- a/packages/kit/src/vite/index.js +++ b/packages/kit/src/vite/index.js @@ -180,7 +180,7 @@ function kit() { paths = { build_dir: `${svelte_config.kit.outDir}/build`, output_dir: `${svelte_config.kit.outDir}/output`, - client_out_dir: `${svelte_config.kit.outDir}/output/client/${svelte_config.kit.appDir}` + client_out_dir: `${svelte_config.kit.outDir}/output/client` }; if (is_build) { @@ -209,6 +209,7 @@ function kit() { input: `${get_runtime_directory(svelte_config.kit)}/client/start.js` } }, + publicDir: svelte_config.kit.files.assets, resolve: { alias: get_aliases(svelte_config.kit) }, @@ -317,8 +318,8 @@ function kit() { const files = new Set([ ...static_files, - ...chunks.map((chunk) => `${svelte_config.kit.appDir}/${chunk.fileName}`), - ...assets.map((chunk) => `${svelte_config.kit.appDir}/${chunk.fileName}`) + ...chunks.map((chunk) => chunk.fileName), + ...assets.map((chunk) => chunk.fileName) ]); // TODO is this right? diff --git a/packages/kit/src/vite/preview/index.js b/packages/kit/src/vite/preview/index.js index 078e6146ed3a..d6bfcede5f7e 100644 --- a/packages/kit/src/vite/preview/index.js +++ b/packages/kit/src/vite/preview/index.js @@ -44,9 +44,6 @@ export async function preview(vite, config, protocol) { const server = new Server(manifest); return () => { - // files in `static` - vite.middlewares.use(scoped(assets, mutable(config.kit.files.assets))); - // immutable generated client assets vite.middlewares.use( scoped( From e1d95291e55692cf832becaae92ac90563479375 Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Mon, 18 Jul 2022 14:27:14 -0700 Subject: [PATCH 02/31] fixes --- packages/kit/src/vite/build/build_service_worker.js | 2 +- packages/kit/src/vite/index.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/kit/src/vite/build/build_service_worker.js b/packages/kit/src/vite/build/build_service_worker.js index 4f4335a27f9e..9ae4441479e3 100644 --- a/packages/kit/src/vite/build/build_service_worker.js +++ b/packages/kit/src/vite/build/build_service_worker.js @@ -43,7 +43,7 @@ export async function build_service_worker( export const build = [ ${Array.from(build) - .map((file) => `${s(`${config.kit.paths.base}/${config.kit.appDir}/${file}`)}`) + .map((file) => `${s(`${config.kit.paths.base}/${file}`)}`) .join(',\n\t\t\t\t')} ]; diff --git a/packages/kit/src/vite/index.js b/packages/kit/src/vite/index.js index b89a40de1ea3..0d50488390cf 100644 --- a/packages/kit/src/vite/index.js +++ b/packages/kit/src/vite/index.js @@ -273,7 +273,7 @@ function kit() { }); fs.writeFileSync( - `${paths.client_out_dir}/version.json`, + `${paths.client_out_dir}/${svelte_config.kit.appDir}/version.json`, JSON.stringify({ version: process.env.VITE_SVELTEKIT_APP_VERSION }) ); From 1eebe2e24ea83a70b369007bd02f7c0178d53216 Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Mon, 18 Jul 2022 14:51:24 -0700 Subject: [PATCH 03/31] fix --- packages/kit/src/vite/build/build_server.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/kit/src/vite/build/build_server.js b/packages/kit/src/vite/build/build_server.js index 38e006d64c84..506a5c169de0 100644 --- a/packages/kit/src/vite/build/build_server.js +++ b/packages/kit/src/vite/build/build_server.js @@ -73,7 +73,7 @@ export class Server { manifest, method_override: ${s(config.kit.methodOverride)}, paths: { base, assets }, - prefix: '/' + assets, + prefix: assets || '/', prerender: { default: ${config.kit.prerender.default}, enabled: ${config.kit.prerender.enabled} From 9600c7b9898c037374b28db498d609ae68ece9b9 Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Mon, 18 Jul 2022 14:58:22 -0700 Subject: [PATCH 04/31] third time's the charm --- packages/kit/src/vite/build/build_server.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/kit/src/vite/build/build_server.js b/packages/kit/src/vite/build/build_server.js index 506a5c169de0..fdab8381befd 100644 --- a/packages/kit/src/vite/build/build_server.js +++ b/packages/kit/src/vite/build/build_server.js @@ -73,7 +73,7 @@ export class Server { manifest, method_override: ${s(config.kit.methodOverride)}, paths: { base, assets }, - prefix: assets || '/', + prefix: assets + '/', prerender: { default: ${config.kit.prerender.default}, enabled: ${config.kit.prerender.enabled} From 63724efea3ef7488dd8ffb6227a64abbd3cdc26f Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Mon, 18 Jul 2022 15:23:49 -0700 Subject: [PATCH 05/31] finish merge --- packages/kit/src/vite/index.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/packages/kit/src/vite/index.js b/packages/kit/src/vite/index.js index 31c090cfc32b..b1ff98851b35 100644 --- a/packages/kit/src/vite/index.js +++ b/packages/kit/src/vite/index.js @@ -205,13 +205,10 @@ function kit() { input: `${get_runtime_directory(svelte_config.kit)}/client/start.js` } }, -<<<<<<< HEAD - publicDir: svelte_config.kit.files.assets, -======= define: { __SVELTEKIT_APP_VERSION_POLL_INTERVAL__: '0' }, ->>>>>>> master + publicDir: svelte_config.kit.files.assets, resolve: { alias: get_aliases(svelte_config.kit) }, From 08edcc6ea53761ea574f3961eb54d9d0dddc02b8 Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Mon, 18 Jul 2022 15:43:16 -0700 Subject: [PATCH 06/31] remove writeStatic --- .changeset/four-pandas-push.md | 2 +- packages/adapter-cloudflare-workers/index.js | 1 - packages/adapter-cloudflare/index.js | 1 - packages/adapter-netlify/index.js | 3 +-- packages/adapter-node/index.js | 2 -- packages/adapter-static/index.js | 1 - packages/kit/src/core/adapt/builder.js | 4 ---- packages/kit/src/core/adapt/builder.spec.js | 11 ----------- packages/kit/types/index.d.ts | 5 ----- 9 files changed, 2 insertions(+), 28 deletions(-) diff --git a/.changeset/four-pandas-push.md b/.changeset/four-pandas-push.md index 52d7c5e37aca..8f7df90ea004 100644 --- a/.changeset/four-pandas-push.md +++ b/.changeset/four-pandas-push.md @@ -2,4 +2,4 @@ '@sveltejs/kit': patch --- -set Vite's publicDir option and change Vite's output dir +[breaking] set Vite's `publicDir` option and change Vite's output dir diff --git a/packages/adapter-cloudflare-workers/index.js b/packages/adapter-cloudflare-workers/index.js index 7bfd7634828a..c8830c12bfa7 100644 --- a/packages/adapter-cloudflare-workers/index.js +++ b/packages/adapter-cloudflare-workers/index.js @@ -74,7 +74,6 @@ export default function () { builder.log.minor('Copying assets...'); builder.writeClient(site.bucket); - builder.writeStatic(site.bucket); builder.writePrerendered(site.bucket); } }; diff --git a/packages/adapter-cloudflare/index.js b/packages/adapter-cloudflare/index.js index 41c4f2477f68..bfb9d968aa1f 100644 --- a/packages/adapter-cloudflare/index.js +++ b/packages/adapter-cloudflare/index.js @@ -23,7 +23,6 @@ export default function () { builder.rimraf(tmp); builder.mkdirp(tmp); - builder.writeStatic(dest); builder.writeClient(dest); builder.writePrerendered(dest); diff --git a/packages/adapter-netlify/index.js b/packages/adapter-netlify/index.js index f1fbf9b94185..39bc32738954 100644 --- a/packages/adapter-netlify/index.js +++ b/packages/adapter-netlify/index.js @@ -55,7 +55,6 @@ export default function ({ split = false, edge = edge_set_in_env_var } = {}) { builder.log.minor(`Publishing to "${publish}"`); builder.log.minor('Copying assets...'); - builder.writeStatic(publish); builder.writeClient(publish); builder.writePrerendered(publish); @@ -232,7 +231,7 @@ async function generate_lambda_functions({ builder, publish, split, esm }) { redirects.push('* /.netlify/functions/render 200'); } - // this should happen at the end, after builder.writeStatic(...), + // this should happen at the end, after builder.writeClient(...), // so that generated redirects are appended to custom redirects // rather than replaced by them builder.log.minor('Writing redirects...'); diff --git a/packages/adapter-node/index.js b/packages/adapter-node/index.js index baccdcbb2a6c..4dbd7d1bd9ad 100644 --- a/packages/adapter-node/index.js +++ b/packages/adapter-node/index.js @@ -30,7 +30,6 @@ export default function (opts = {}) { builder.log.minor('Copying assets'); builder.writeClient(`${out}/client`); builder.writeServer(`${out}/server`); - builder.writeStatic(`${out}/static`); builder.writePrerendered(`${out}/prerendered`); writeFileSync( @@ -51,7 +50,6 @@ export default function (opts = {}) { if (precompress) { builder.log.minor('Compressing assets'); await compress(`${out}/client`); - await compress(`${out}/static`); await compress(`${out}/prerendered`); } } diff --git a/packages/adapter-static/index.js b/packages/adapter-static/index.js index 3d5d3d3aac98..6120ff5f712c 100644 --- a/packages/adapter-static/index.js +++ b/packages/adapter-static/index.js @@ -43,7 +43,6 @@ export default function (options) { builder.rimraf(assets); builder.rimraf(pages); - builder.writeStatic(assets); builder.writeClient(assets); builder.writePrerendered(pages, { fallback }); diff --git a/packages/kit/src/core/adapt/builder.js b/packages/kit/src/core/adapt/builder.js index 7490e48342ae..b49bfc4e8190 100644 --- a/packages/kit/src/core/adapt/builder.js +++ b/packages/kit/src/core/adapt/builder.js @@ -141,10 +141,6 @@ export function create_builder({ config, build_data, prerendered, log }) { return copy(`${config.kit.outDir}/output/server`, dest); }, - writeStatic(dest) { - return copy(config.kit.files.assets, dest); - }, - // @ts-expect-error async prerender() { throw new Error( diff --git a/packages/kit/src/core/adapt/builder.spec.js b/packages/kit/src/core/adapt/builder.spec.js index 2b05b1107317..2f2c82f4a78f 100644 --- a/packages/kit/src/core/adapt/builder.spec.js +++ b/packages/kit/src/core/adapt/builder.spec.js @@ -39,17 +39,6 @@ test('copy files', () => { const dest = join(__dirname, 'output'); - rmSync(dest, { recursive: true, force: true }); - builder.writeStatic(dest); - - assert.equal( - glob('**', { - cwd: /** @type {import('types').ValidatedConfig} */ (mocked).kit.files.assets, - dot: true - }), - glob('**', { cwd: dest, dot: true }) - ); - rmSync(dest, { recursive: true, force: true }); builder.writeClient(dest); diff --git a/packages/kit/types/index.d.ts b/packages/kit/types/index.d.ts index 97ff605ca3ec..b06f2b444917 100644 --- a/packages/kit/types/index.d.ts +++ b/packages/kit/types/index.d.ts @@ -65,11 +65,6 @@ export interface Builder { * @returns an array of paths corresponding to the files that have been created by the copy */ writeServer(dest: string): string[]; - /** - * @param dest the destination folder to which files should be copied - * @returns an array of paths corresponding to the files that have been created by the copy - */ - writeStatic(dest: string): string[]; /** * @param from the source file or folder * @param to the destination file or folder From 3073800b4498cfec5d8294ef50dfade4fd890cfd Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Mon, 18 Jul 2022 18:47:17 -0400 Subject: [PATCH 07/31] missed a spot --- packages/adapter-vercel/index.js | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/adapter-vercel/index.js b/packages/adapter-vercel/index.js index b8ea4f9d9e21..fc55c079f45d 100644 --- a/packages/adapter-vercel/index.js +++ b/packages/adapter-vercel/index.js @@ -236,7 +236,6 @@ export default function ({ external = [], edge, split } = {}) { builder.log.minor('Copying assets...'); - builder.writeStatic(dirs.static); builder.writeClient(dirs.static); builder.writePrerendered(dirs.static); From 8600c3820dd1e1415daa992394381c11b3993c82 Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Mon, 18 Jul 2022 16:42:14 -0700 Subject: [PATCH 08/31] fix options-2 tests by using Vite's base option --- packages/kit/src/core/prerender/prerender.js | 13 ++-- packages/kit/src/runtime/server/index.js | 7 --- packages/kit/src/vite/dev/index.js | 13 ---- packages/kit/src/vite/index.js | 2 +- packages/kit/src/vite/preview/index.js | 60 ++++++++----------- packages/kit/test/apps/options-2/test/test.js | 4 +- 6 files changed, 32 insertions(+), 67 deletions(-) diff --git a/packages/kit/src/core/prerender/prerender.js b/packages/kit/src/core/prerender/prerender.js index e14a25760756..5dcf3635118b 100644 --- a/packages/kit/src/core/prerender/prerender.js +++ b/packages/kit/src/core/prerender/prerender.js @@ -84,7 +84,7 @@ export async function prerender({ config, entries, files, log }) { * @param {boolean} is_html */ function output_filename(path, is_html) { - const file = path.slice(config.paths.base.length + 1); + const file = path; if (file === '') { return 'index.html'; @@ -109,7 +109,7 @@ export async function prerender({ config, entries, files, log }) { if (seen.has(decoded)) return; seen.add(decoded); - const file = decoded.slice(config.paths.base.length + 1); + const file = decoded; if (files.has(file)) return; return q.add(() => visit(decoded, encoded || encodeURI(decoded), referrer)); @@ -121,11 +121,6 @@ export async function prerender({ config, entries, files, log }) { * @param {string?} referrer */ async function visit(decoded, encoded, referrer) { - if (!decoded.startsWith(config.paths.base)) { - error({ status: 404, path: decoded, referrer, referenceType: 'linked' }); - return; - } - /** @type {Map} */ const dependencies = new Map(); @@ -259,10 +254,10 @@ export async function prerender({ config, entries, files, log }) { for (const entry of config.prerender.entries) { if (entry === '*') { for (const entry of entries) { - enqueue(null, config.paths.base + entry); // TODO can we pre-normalize these? + enqueue(null, entry); // TODO can we pre-normalize these? } } else { - enqueue(null, config.paths.base + entry); + enqueue(null, entry); } } diff --git a/packages/kit/src/runtime/server/index.js b/packages/kit/src/runtime/server/index.js index d149441a0cfb..d4bb0e2d4363 100644 --- a/packages/kit/src/runtime/server/index.js +++ b/packages/kit/src/runtime/server/index.js @@ -57,13 +57,6 @@ export async function respond(request, options, state) { /** @type {Record} */ let params = {}; - if (options.paths.base && !state.prerendering?.fallback) { - if (!decoded.startsWith(options.paths.base)) { - return new Response('Not found', { status: 404 }); - } - decoded = decoded.slice(options.paths.base.length) || '/'; - } - const is_data_request = decoded.endsWith(DATA_SUFFIX); if (is_data_request) { diff --git a/packages/kit/src/vite/dev/index.js b/packages/kit/src/vite/dev/index.js index 648293266248..800678eda7d9 100644 --- a/packages/kit/src/vite/dev/index.js +++ b/packages/kit/src/vite/dev/index.js @@ -202,13 +202,6 @@ export async function dev(vite, vite_config, svelte_config) { return; } - if (!decoded.startsWith(svelte_config.kit.paths.base)) { - return not_found( - res, - `Not found (did you mean ${svelte_config.kit.paths.base + req.url}?)` - ); - } - /** @type {Partial} */ const user_hooks = resolve_entry(svelte_config.kit.files.hooks) ? await vite.ssrLoadModule(`/${svelte_config.kit.files.hooks}`) @@ -362,12 +355,6 @@ export async function dev(vite, vite_config, svelte_config) { }; } -/** @param {import('http').ServerResponse} res */ -function not_found(res, message = 'Not found') { - res.statusCode = 404; - res.end(message); -} - /** * @param {import('connect').Server} server */ diff --git a/packages/kit/src/vite/index.js b/packages/kit/src/vite/index.js index b1ff98851b35..53208d3d6478 100644 --- a/packages/kit/src/vite/index.js +++ b/packages/kit/src/vite/index.js @@ -197,7 +197,7 @@ function kit() { /** @type {import('vite').UserConfig} */ const result = { appType: 'custom', - base: '/', + base: `${svelte_config.kit.paths.base}/`, build: { rollupOptions: { // Vite dependency crawler needs an explicit JS entry point diff --git a/packages/kit/src/vite/preview/index.js b/packages/kit/src/vite/preview/index.js index d6bfcede5f7e..09eab1d5b22d 100644 --- a/packages/kit/src/vite/preview/index.js +++ b/packages/kit/src/vite/preview/index.js @@ -59,18 +59,6 @@ export async function preview(vite, config, protocol) { ) ); - vite.middlewares.use((req, res, next) => { - const original_url = /** @type {string} */ (req.url); - const { pathname } = new URL(original_url, 'http://dummy'); - - if (pathname.startsWith(base)) { - next(); - } else { - res.statusCode = 404; - res.end(`Not found (did you mean ${base + pathname}?)`); - } - }); - // prerendered dependencies vite.middlewares.use( scoped(base, mutable(join(config.kit.outDir, 'output/prerendered/dependencies'))) @@ -119,29 +107,31 @@ export async function preview(vite, config, protocol) { ); // SSR - vite.middlewares.use(async (req, res) => { - const host = req.headers['host']; - - let request; - - try { - request = await getRequest(`${protocol}://${host}`, req); - } catch (/** @type {any} */ err) { - res.statusCode = err.status || 400; - return res.end(err.reason || 'Invalid request body'); - } - - setResponse( - res, - await server.respond(request, { - getClientAddress: () => { - const { remoteAddress } = req.socket; - if (remoteAddress) return remoteAddress; - throw new Error('Could not determine clientAddress'); - } - }) - ); - }); + vite.middlewares.use( + scoped(base, async (req, res) => { + const host = req.headers['host']; + + let request; + + try { + request = await getRequest(`${protocol}://${host}`, req); + } catch (/** @type {any} */ err) { + res.statusCode = err.status || 400; + return res.end(err.reason || 'Invalid request body'); + } + + setResponse( + res, + await server.respond(request, { + getClientAddress: () => { + const { remoteAddress } = req.socket; + if (remoteAddress) return remoteAddress; + throw new Error('Could not determine clientAddress'); + } + }) + ); + }) + ); }; } diff --git a/packages/kit/test/apps/options-2/test/test.js b/packages/kit/test/apps/options-2/test/test.js index 9ca9a0eb5bf0..fdea1e23026f 100644 --- a/packages/kit/test/apps/options-2/test/test.js +++ b/packages/kit/test/apps/options-2/test/test.js @@ -7,7 +7,7 @@ test.describe.configure({ mode: 'parallel' }); test.describe('paths.base', () => { test('serves /basepath', async ({ page }) => { - await page.goto('/basepath'); + await page.goto('/basepath/'); expect(await page.textContent('h1')).toBe('Hello'); }); @@ -28,7 +28,7 @@ test.describe('Service worker', () => { }); test('does not register /basepath/service-worker.js', async ({ page }) => { - await page.goto('/basepath'); + await page.goto('/basepath/'); expect(await page.content()).not.toMatch(/navigator\.serviceWorker/); }); }); From da434e93d00fbe23276c4572c3c15d1a41013ac0 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Mon, 18 Jul 2022 20:09:59 -0400 Subject: [PATCH 09/31] keep writeStatic, but throw --- packages/kit/src/core/adapt/builder.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/packages/kit/src/core/adapt/builder.js b/packages/kit/src/core/adapt/builder.js index b49bfc4e8190..f49ec98f273c 100644 --- a/packages/kit/src/core/adapt/builder.js +++ b/packages/kit/src/core/adapt/builder.js @@ -141,6 +141,16 @@ export function create_builder({ config, build_data, prerendered, log }) { return copy(`${config.kit.outDir}/output/server`, dest); }, + // TODO remove these methods for 1.0 + // @ts-expect-error + writeStatic(dest) { + throw new Error( + `writeStatic has been removed. Please ensure you are using the latest version of ${ + config.kit.adapter.name || 'your adapter' + }` + ); + }, + // @ts-expect-error async prerender() { throw new Error( From 5edd5f3f66a4da9d8871f40d70453959e3de58b2 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Mon, 18 Jul 2022 20:46:33 -0400 Subject: [PATCH 10/31] shruggie --- packages/kit/src/core/adapt/builder.js | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/kit/src/core/adapt/builder.js b/packages/kit/src/core/adapt/builder.js index f49ec98f273c..8bead7d24223 100644 --- a/packages/kit/src/core/adapt/builder.js +++ b/packages/kit/src/core/adapt/builder.js @@ -151,7 +151,6 @@ export function create_builder({ config, build_data, prerendered, log }) { ); }, - // @ts-expect-error async prerender() { throw new Error( 'builder.prerender() has been removed. Prerendering now takes place in the build phase — see builder.prerender and builder.writePrerendered' From e4e9033fc5e92960a1dac519d89bfe3a395e2a72 Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Mon, 18 Jul 2022 18:30:11 -0700 Subject: [PATCH 11/31] update changeset --- .changeset/four-pandas-push.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.changeset/four-pandas-push.md b/.changeset/four-pandas-push.md index 8f7df90ea004..9cfa67882de7 100644 --- a/.changeset/four-pandas-push.md +++ b/.changeset/four-pandas-push.md @@ -1,5 +1,11 @@ --- '@sveltejs/kit': patch +'@sveltejs/adapter-cloudflare-workers': patch +'@sveltejs/adapter-cloudflare': patch +'@sveltejs/adapter-netlify': patch +'@sveltejs/adapter-node': patch +'@sveltejs/adapter-static': patch +'@sveltejs/adapter-vercel': patch --- [breaking] set Vite's `publicDir` option and change Vite's output dir From 1f65e054fb5705cf95db144e07e6e12d96dc104d Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Mon, 18 Jul 2022 20:16:08 -0700 Subject: [PATCH 12/31] trailing slash + vite base compat --- packages/kit/src/runtime/server/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/kit/src/runtime/server/index.js b/packages/kit/src/runtime/server/index.js index d4bb0e2d4363..a1a69c8196fd 100644 --- a/packages/kit/src/runtime/server/index.js +++ b/packages/kit/src/runtime/server/index.js @@ -86,13 +86,14 @@ export async function respond(request, options, state) { const normalized = normalize_path(url.pathname, options.trailing_slash); if (normalized !== url.pathname && !state.prerendering?.fallback) { + const path = options.paths.base + normalized; return new Response(undefined, { status: 301, headers: { 'x-sveltekit-normalize': '1', location: // ensure paths starting with '//' are not treated as protocol-relative - (normalized.startsWith('//') ? url.origin + normalized : normalized) + + (normalized.startsWith('//') ? url.origin + path : path) + (url.search === '?' ? '' : url.search) } }); From 9aed215f6ba07c33f483e7a7a678d4bcc8296f25 Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Mon, 18 Jul 2022 20:24:32 -0700 Subject: [PATCH 13/31] can't figure this one out. push to CI and see if it gives the same error --- packages/kit/test/apps/options/test/test.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/kit/test/apps/options/test/test.js b/packages/kit/test/apps/options/test/test.js index c7af4695c15b..2cec50a8534f 100644 --- a/packages/kit/test/apps/options/test/test.js +++ b/packages/kit/test/apps/options/test/test.js @@ -7,9 +7,11 @@ test.describe.configure({ mode: 'parallel' }); test.describe('base path', () => { test('serves a useful 404 when visiting unprefixed path', async ({ request }) => { - const response = await request.get('/'); + const response = await request.get('/slash/'); expect(response.status()).toBe(404); - expect(await response.text()).toBe('Not found (did you mean /path-base/?)'); + expect(await response.text()).toBe( + 'The server is configured with a public base URL of /path-base/ - did you mean to visit /path-base/slash/ instead?' + ); }); test('serves /', async ({ page, javaScriptEnabled }) => { From 4521b11239d9d21fff5f35ea8acea463976bf615 Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Mon, 18 Jul 2022 20:27:17 -0700 Subject: [PATCH 14/31] probably don't need this TODO anymore --- packages/kit/src/core/prerender/prerender.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/kit/src/core/prerender/prerender.js b/packages/kit/src/core/prerender/prerender.js index 5dcf3635118b..86255e096ef2 100644 --- a/packages/kit/src/core/prerender/prerender.js +++ b/packages/kit/src/core/prerender/prerender.js @@ -254,7 +254,7 @@ export async function prerender({ config, entries, files, log }) { for (const entry of config.prerender.entries) { if (entry === '*') { for (const entry of entries) { - enqueue(null, entry); // TODO can we pre-normalize these? + enqueue(null, entry); } } else { enqueue(null, entry); From f1e22db675a5a9443ee90f11921a779ef824efda Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Mon, 18 Jul 2022 21:19:02 -0700 Subject: [PATCH 15/31] remove outdated test --- packages/kit/test/apps/basics/test/server.test.js | 5 ----- 1 file changed, 5 deletions(-) diff --git a/packages/kit/test/apps/basics/test/server.test.js b/packages/kit/test/apps/basics/test/server.test.js index 80b9337b7cd3..1a0ceeb7490d 100644 --- a/packages/kit/test/apps/basics/test/server.test.js +++ b/packages/kit/test/apps/basics/test/server.test.js @@ -470,11 +470,6 @@ test.describe('Static files', () => { expect(await r2.json()).toEqual({ works: true }); }); - test('Filenames are case-sensitive', async ({ request }) => { - const response = await request.get('/static.JSON'); - expect(response.status()).toBe(404); - }); - test('Serves symlinked asset', async ({ request }) => { const response = await request.get('/symlink-from/hello.txt'); expect(response.status()).toBe(200); From c7bc94e5bcca4f255eb3f7853f31a29cd65b65fd Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Mon, 18 Jul 2022 21:33:07 -0700 Subject: [PATCH 16/31] crawling improvement --- packages/kit/src/core/prerender/crawl.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/kit/src/core/prerender/crawl.js b/packages/kit/src/core/prerender/crawl.js index b74e558d6e60..bb28095912d1 100644 --- a/packages/kit/src/core/prerender/crawl.js +++ b/packages/kit/src/core/prerender/crawl.js @@ -72,7 +72,7 @@ export function crawl(html) { const tag = html.slice(start, i).toUpperCase(); - if (tag === 'SCRIPT' || tag === 'STYLE') { + if (tag === 'SCRIPT' || tag === 'STYLE' || tag === 'LINK') { while (i < html.length) { if ( html[i] === '<' && From 0c7c5815ae72a2f4590e85fb76d814a26ec694b6 Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Mon, 18 Jul 2022 21:48:58 -0700 Subject: [PATCH 17/31] Revert "crawling improvement" This reverts commit c7bc94e5bcca4f255eb3f7853f31a29cd65b65fd. --- packages/kit/src/core/prerender/crawl.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/kit/src/core/prerender/crawl.js b/packages/kit/src/core/prerender/crawl.js index bb28095912d1..b74e558d6e60 100644 --- a/packages/kit/src/core/prerender/crawl.js +++ b/packages/kit/src/core/prerender/crawl.js @@ -72,7 +72,7 @@ export function crawl(html) { const tag = html.slice(start, i).toUpperCase(); - if (tag === 'SCRIPT' || tag === 'STYLE' || tag === 'LINK') { + if (tag === 'SCRIPT' || tag === 'STYLE') { while (i < html.length) { if ( html[i] === '<' && From 9ec256e16180273fd043054caf295bff305a14d7 Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Tue, 19 Jul 2022 10:32:06 -0700 Subject: [PATCH 18/31] update changelog --- .changeset/four-pandas-push.md | 6 ------ 1 file changed, 6 deletions(-) diff --git a/.changeset/four-pandas-push.md b/.changeset/four-pandas-push.md index 9cfa67882de7..8f7df90ea004 100644 --- a/.changeset/four-pandas-push.md +++ b/.changeset/four-pandas-push.md @@ -1,11 +1,5 @@ --- '@sveltejs/kit': patch -'@sveltejs/adapter-cloudflare-workers': patch -'@sveltejs/adapter-cloudflare': patch -'@sveltejs/adapter-netlify': patch -'@sveltejs/adapter-node': patch -'@sveltejs/adapter-static': patch -'@sveltejs/adapter-vercel': patch --- [breaking] set Vite's `publicDir` option and change Vite's output dir From f01e43772c72a18b5ba69db076f0af227a9ee427 Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Tue, 19 Jul 2022 10:34:45 -0700 Subject: [PATCH 19/31] update changeset --- .changeset/four-pandas-push.md | 1 + 1 file changed, 1 insertion(+) diff --git a/.changeset/four-pandas-push.md b/.changeset/four-pandas-push.md index 8f7df90ea004..27c20caa3e12 100644 --- a/.changeset/four-pandas-push.md +++ b/.changeset/four-pandas-push.md @@ -1,5 +1,6 @@ --- '@sveltejs/kit': patch +'@sveltejs/adapter-node': patch --- [breaking] set Vite's `publicDir` option and change Vite's output dir From 5b62370d42742ed80e9109ddbd844221aef132e0 Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Tue, 19 Jul 2022 12:55:05 -0700 Subject: [PATCH 20/31] cleanup from merging master --- .changeset/four-pandas-push.md | 2 +- packages/kit/src/core/generate_manifest/index.js | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.changeset/four-pandas-push.md b/.changeset/four-pandas-push.md index 27c20caa3e12..acc2609b0cd4 100644 --- a/.changeset/four-pandas-push.md +++ b/.changeset/four-pandas-push.md @@ -3,4 +3,4 @@ '@sveltejs/adapter-node': patch --- -[breaking] set Vite's `publicDir` option and change Vite's output dir +[breaking] set Vite's `publicDir` and `base` options diff --git a/packages/kit/src/core/generate_manifest/index.js b/packages/kit/src/core/generate_manifest/index.js index ac61b4f21269..983c8501e99c 100644 --- a/packages/kit/src/core/generate_manifest/index.js +++ b/packages/kit/src/core/generate_manifest/index.js @@ -64,6 +64,7 @@ export function generate_manifest({ build_data, relative_path, routes, format = // prettier-ignore return `{ + appDir: ${s(build_data.app_dir)}, assets: new Set(${s(assets)}), mimeTypes: ${s(get_mime_lookup(build_data.manifest_data))}, _: { From ded83dadf335e8dd39860c97674995cb4a2b6415 Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Tue, 19 Jul 2022 12:56:06 -0700 Subject: [PATCH 21/31] restore TODO --- packages/kit/src/core/prerender/prerender.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/kit/src/core/prerender/prerender.js b/packages/kit/src/core/prerender/prerender.js index 9d5787fd4dd1..eee247885670 100644 --- a/packages/kit/src/core/prerender/prerender.js +++ b/packages/kit/src/core/prerender/prerender.js @@ -254,7 +254,7 @@ export async function prerender({ config, entries, files, log }) { for (const entry of config.prerender.entries) { if (entry === '*') { for (const entry of entries) { - enqueue(null, entry); + enqueue(null, entry); // TODO can we pre-normalize these? } } else { enqueue(null, entry); From c1457a2535fef5963880fe9fc9935e280a243231 Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Tue, 19 Jul 2022 14:20:35 -0700 Subject: [PATCH 22/31] format --- packages/kit/src/core/adapt/builder.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/kit/src/core/adapt/builder.js b/packages/kit/src/core/adapt/builder.js index 8e1c2f1b4b97..d0b5dad7ffdb 100644 --- a/packages/kit/src/core/adapt/builder.js +++ b/packages/kit/src/core/adapt/builder.js @@ -122,9 +122,7 @@ export function create_builder({ config, build_data, prerendered, log }) { }, writeClient(dest) { - return [ - ...copy(`${config.kit.outDir}/output/client`, dest) - ]; + return [...copy(`${config.kit.outDir}/output/client`, dest)]; }, writePrerendered(dest, { fallback } = {}) { From e74f728168c905914d2f3996b0b92bba9194eb85 Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Tue, 19 Jul 2022 15:11:30 -0700 Subject: [PATCH 23/31] fix adapter-static tests --- packages/kit/src/core/prerender/prerender.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/kit/src/core/prerender/prerender.js b/packages/kit/src/core/prerender/prerender.js index eee247885670..767af79b2219 100644 --- a/packages/kit/src/core/prerender/prerender.js +++ b/packages/kit/src/core/prerender/prerender.js @@ -84,7 +84,7 @@ export async function prerender({ config, entries, files, log }) { * @param {boolean} is_html */ function output_filename(path, is_html) { - const file = path; + const file = path.slice(1); if (file === '') { return 'index.html'; @@ -109,7 +109,7 @@ export async function prerender({ config, entries, files, log }) { if (seen.has(decoded)) return; seen.add(decoded); - const file = decoded; + const file = decoded.slice(1); if (files.has(file)) return; return q.add(() => visit(decoded, encoded || encodeURI(decoded), referrer)); From a38275556b9703269399eccdc4b8b836b7ade251 Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Tue, 19 Jul 2022 15:14:46 -0700 Subject: [PATCH 24/31] fix unit tests --- packages/kit/src/core/adapt/builder.spec.js | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/packages/kit/src/core/adapt/builder.spec.js b/packages/kit/src/core/adapt/builder.spec.js index 0af84ddec6c5..2f2c82f4a78f 100644 --- a/packages/kit/src/core/adapt/builder.spec.js +++ b/packages/kit/src/core/adapt/builder.spec.js @@ -43,13 +43,7 @@ test('copy files', () => { builder.writeClient(dest); assert.equal( - [ - ...glob('**', { - cwd: /** @type {import('types').ValidatedConfig} */ (mocked).kit.files.assets, - dot: true - }), - ...glob('**', { cwd: `${outDir}/output/client`, dot: true }) - ], + glob('**', { cwd: `${outDir}/output/client`, dot: true }), glob('**', { cwd: dest, dot: true }) ); From eee66db750916aebc1103782ffafdf2e559910c2 Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Tue, 19 Jul 2022 15:36:24 -0700 Subject: [PATCH 25/31] fix options test --- packages/kit/test/apps/options/test/test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/kit/test/apps/options/test/test.js b/packages/kit/test/apps/options/test/test.js index 2cec50a8534f..30fa1c4cd38c 100644 --- a/packages/kit/test/apps/options/test/test.js +++ b/packages/kit/test/apps/options/test/test.js @@ -7,10 +7,10 @@ test.describe.configure({ mode: 'parallel' }); test.describe('base path', () => { test('serves a useful 404 when visiting unprefixed path', async ({ request }) => { - const response = await request.get('/slash/'); + const response = await request.get('/slash/', { headers: { Accept: 'text/html' } }); expect(response.status()).toBe(404); expect(await response.text()).toBe( - 'The server is configured with a public base URL of /path-base/ - did you mean to visit /path-base/slash/ instead?' + 'The server is configured with a public base URL of /path-base/ - did you mean to visit /path-base/slash/ instead?' ); }); From 14b7875c7cb3cea1e5bff4f779bd6519d5ba1d15 Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Tue, 19 Jul 2022 15:39:30 -0700 Subject: [PATCH 26/31] format --- packages/kit/test/apps/options/test/test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/kit/test/apps/options/test/test.js b/packages/kit/test/apps/options/test/test.js index 30fa1c4cd38c..565e43262dfe 100644 --- a/packages/kit/test/apps/options/test/test.js +++ b/packages/kit/test/apps/options/test/test.js @@ -10,7 +10,7 @@ test.describe('base path', () => { const response = await request.get('/slash/', { headers: { Accept: 'text/html' } }); expect(response.status()).toBe(404); expect(await response.text()).toBe( - 'The server is configured with a public base URL of /path-base/ - did you mean to visit /path-base/slash/ instead?' + 'The server is configured with a public base URL of /path-base/ - did you mean to visit /path-base/slash/ instead?' ); }); From 774b70cd75101fed0b31151a983a902e397543e6 Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Tue, 19 Jul 2022 16:30:09 -0700 Subject: [PATCH 27/31] prerender updates --- packages/kit/src/core/prerender/prerender.js | 30 ++++++++++++-------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/packages/kit/src/core/prerender/prerender.js b/packages/kit/src/core/prerender/prerender.js index 767af79b2219..a16cdbc83140 100644 --- a/packages/kit/src/core/prerender/prerender.js +++ b/packages/kit/src/core/prerender/prerender.js @@ -84,7 +84,7 @@ export async function prerender({ config, entries, files, log }) { * @param {boolean} is_html */ function output_filename(path, is_html) { - const file = path.slice(1); + const file = path.slice(config.paths.base.length + 1); if (file === '') { return 'index.html'; @@ -103,28 +103,32 @@ export async function prerender({ config, entries, files, log }) { /** * @param {string | null} referrer * @param {string} decoded - * @param {string} [encoded] */ - function enqueue(referrer, decoded, encoded) { + function enqueue(referrer, decoded) { if (seen.has(decoded)) return; seen.add(decoded); - const file = decoded.slice(1); + const file = decoded.slice(config.paths.base.length + 1); if (files.has(file)) return; - return q.add(() => visit(decoded, encoded || encodeURI(decoded), referrer)); + return q.add(() => visit(decoded, referrer)); } /** * @param {string} decoded - * @param {string} encoded * @param {string?} referrer */ - async function visit(decoded, encoded, referrer) { + async function visit(decoded, referrer) { + if (!decoded.startsWith(config.paths.base)) { + error({ status: 404, path: decoded, referrer, referenceType: 'linked' }); + return; + } + /** @type {Map} */ const dependencies = new Map(); - const response = await server.respond(new Request(`http://sveltekit-prerender${encoded}`), { + const path = encodeURI(decoded.slice(config.paths.base.length)); + const response = await server.respond(new Request(`http://sveltekit-prerender${path}`), { getClientAddress, prerendering: { dependencies @@ -133,6 +137,8 @@ export async function prerender({ config, entries, files, log }) { const body = Buffer.from(await response.arrayBuffer()); + const encoded = encodeURI(decoded); + save('pages', response, body, decoded, encoded, referrer, 'linked'); for (const [dependency_path, result] of dependencies) { @@ -166,7 +172,7 @@ export async function prerender({ config, entries, files, log }) { // TODO warn that query strings have no effect on statically-exported pages } - enqueue(decoded, decodeURI(pathname), pathname); + enqueue(decoded, decodeURI(pathname)); } } } @@ -196,7 +202,7 @@ export async function prerender({ config, entries, files, log }) { if (location) { const resolved = resolve(encoded, location); if (is_root_relative(resolved)) { - enqueue(decoded, decodeURI(resolved), resolved); + enqueue(decoded, decodeURI(resolved)); } if (!response.headers.get('x-sveltekit-normalize')) { @@ -254,10 +260,10 @@ export async function prerender({ config, entries, files, log }) { for (const entry of config.prerender.entries) { if (entry === '*') { for (const entry of entries) { - enqueue(null, entry); // TODO can we pre-normalize these? + enqueue(null, config.paths.base + entry); // TODO can we pre-normalize these? } } else { - enqueue(null, entry); + enqueue(null, config.paths.base + entry); } } From 5ef88c7bc53781cae6808a6b59d117c0435416fc Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Tue, 19 Jul 2022 16:42:17 -0700 Subject: [PATCH 28/31] restore and update preview message --- packages/kit/src/vite/preview/index.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/packages/kit/src/vite/preview/index.js b/packages/kit/src/vite/preview/index.js index 09eab1d5b22d..f22c606157d0 100644 --- a/packages/kit/src/vite/preview/index.js +++ b/packages/kit/src/vite/preview/index.js @@ -64,6 +64,22 @@ export async function preview(vite, config, protocol) { scoped(base, mutable(join(config.kit.outDir, 'output/prerendered/dependencies'))) ); + vite.middlewares.use((req, res, next) => { + const original_url = /** @type {string} */ (req.url); + const { pathname } = new URL(original_url, 'http://dummy'); + + if (pathname.startsWith(base)) { + next(); + } else { + res.statusCode = 404; + res.end( + `The server is configured with a public base URL of ${base}/ - did you mean to visit ${base + pathname} instead?` + ); + } + }); + // prerendered pages (we can't just use sirv because we need to // preserve the correct trailingSlash behaviour) vite.middlewares.use( From 44381e4d867970988de89f1f74a324c21dca3b16 Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Tue, 19 Jul 2022 19:56:52 -0700 Subject: [PATCH 29/31] restore test --- packages/kit/test/apps/basics/test/server.test.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/kit/test/apps/basics/test/server.test.js b/packages/kit/test/apps/basics/test/server.test.js index 394aaf6839b8..a4402c4c0c00 100644 --- a/packages/kit/test/apps/basics/test/server.test.js +++ b/packages/kit/test/apps/basics/test/server.test.js @@ -473,6 +473,11 @@ test.describe('Static files', () => { expect(await r2.json()).toEqual({ works: true }); }); + test('Filenames are case-sensitive', async ({ request }) => { + const response = await request.get('/static.JSON'); + expect(response.status()).toBe(404); + }); + test('Serves symlinked asset', async ({ request }) => { const response = await request.get('/symlink-from/hello.txt'); expect(response.status()).toBe(200); From 2798f9da4b4f19d0a72c2514e1e4bffbaffa7291 Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Wed, 20 Jul 2022 16:14:07 -0700 Subject: [PATCH 30/31] Revert "restore test" This reverts commit 44381e4d867970988de89f1f74a324c21dca3b16. --- packages/kit/test/apps/basics/test/server.test.js | 5 ----- 1 file changed, 5 deletions(-) diff --git a/packages/kit/test/apps/basics/test/server.test.js b/packages/kit/test/apps/basics/test/server.test.js index a4402c4c0c00..394aaf6839b8 100644 --- a/packages/kit/test/apps/basics/test/server.test.js +++ b/packages/kit/test/apps/basics/test/server.test.js @@ -473,11 +473,6 @@ test.describe('Static files', () => { expect(await r2.json()).toEqual({ works: true }); }); - test('Filenames are case-sensitive', async ({ request }) => { - const response = await request.get('/static.JSON'); - expect(response.status()).toBe(404); - }); - test('Serves symlinked asset', async ({ request }) => { const response = await request.get('/symlink-from/hello.txt'); expect(response.status()).toBe(200); From e1d80c8010ec42f1a7db676d2ced2abf60a78d7d Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Wed, 20 Jul 2022 16:14:51 -0700 Subject: [PATCH 31/31] move code back to where it was originally --- packages/kit/src/vite/preview/index.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/kit/src/vite/preview/index.js b/packages/kit/src/vite/preview/index.js index f22c606157d0..03ae86cd0764 100644 --- a/packages/kit/src/vite/preview/index.js +++ b/packages/kit/src/vite/preview/index.js @@ -59,11 +59,6 @@ export async function preview(vite, config, protocol) { ) ); - // prerendered dependencies - vite.middlewares.use( - scoped(base, mutable(join(config.kit.outDir, 'output/prerendered/dependencies'))) - ); - vite.middlewares.use((req, res, next) => { const original_url = /** @type {string} */ (req.url); const { pathname } = new URL(original_url, 'http://dummy'); @@ -80,6 +75,11 @@ export async function preview(vite, config, protocol) { } }); + // prerendered dependencies + vite.middlewares.use( + scoped(base, mutable(join(config.kit.outDir, 'output/prerendered/dependencies'))) + ); + // prerendered pages (we can't just use sirv because we need to // preserve the correct trailingSlash behaviour) vite.middlewares.use(