Repository: sveltejs/kit
Pull request #5996 (2022-08-17 20:25:31)

[chore] remove chokidar as dependency of kit
Now that `svelte-kit package` is no longer a part of core SvelteKit, it no longer needs `chokidar` as a dependency.

### Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
- [ ] It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
- [x] This message body should clearly illustrate what problems it solves.
- [ ] Ideally, include a test that fails without this PR but passes with it.

### Tests
- [x] Run the tests with `pnpm test` and lint the project with `pnpm lint` and `pnpm check`

### Changesets
- [ ] If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running `pnpm changeset` and following the prompts. All changesets should be `patch` until SvelteKit 1.0


changeset-bot[bot]: ###  ⚠️  No Changeset found

Latest commit: fabfa62ac05ae85405a4af80a45b237010a61de7

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. **If these changes should result in a version bump, you need to add a changeset.**

<details><summary>This PR includes no changesets</summary>

  When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

</details>

[Click here to learn what changesets are, and how to add one](https://github.com/changesets/changesets/blob/master/docs/adding-a-changeset.md).

[Click here if you're a maintainer who wants to add a changeset to this PR](https://github.com/Conduitry/kit/new/kit-rm-chokidar?filename=.changeset/twenty-numbers-breathe.md&value=---%0A%22%40sveltejs%2Fkit%22%3A%20patch%0A---%0A%0A%5Bchore%5D%20remove%20chokidar%20as%20dependency%20of%20kit%0A)



Patch:
diff --git a/.changeset/gentle-news-shout.md b/.changeset/gentle-news-shout.md
new file mode 100644
index 000000000..c5a3bcfda
--- /dev/null
+++ b/.changeset/gentle-news-shout.md
@@ -0,0 +1,5 @@
+---
+'@sveltejs/kit': patch
+---
+
+Return a 303 response when a `POST` handler provides a `location`
diff --git a/.changeset/twenty-numbers-breathe.md b/.changeset/twenty-numbers-breathe.md
new file mode 100644
index 000000000..0c37b700d
--- /dev/null
+++ b/.changeset/twenty-numbers-breathe.md
@@ -0,0 +1,5 @@
+---
+"@sveltejs/kit": patch
+---
+
+[chore] remove chokidar as dependency of kit
diff --git a/.changeset/wicked-spoons-leave.md b/.changeset/wicked-spoons-leave.md
new file mode 100644
index 000000000..b83f016ce
--- /dev/null
+++ b/.changeset/wicked-spoons-leave.md
@@ -0,0 +1,5 @@
+---
+'@sveltejs/kit': patch
+---
+
+Use relative asset paths where possible
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index ef527cd88..9f5643fa9 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -48,6 +48,7 @@ You may need to install some dependencies first, e.g. with `npx playwright insta
 If there are tests that fail on the CI, you can retrieve the failed screenshots by going to the summary page of the CI run. You can usually find this by clicking on "Details" of the check results, click "Summary" at the top-left corner, and then scroll to the bottom "Artifacts" section to download the archive.
 
 It is very easy to introduce flakiness in a browser test. If you try to fix the flakiness in a test, you can run it until failure to gain some confidence you've fixed the test with a command like:
+
 ```
 npx playwright test --workers=1 --repeat-each 1000 --max-failures 1 -g "accepts a Request object"
 ```
@@ -60,7 +61,7 @@ If you would like to test local changes to Vite or another dependency, you can b
 {
 	// ...
 	"dependencies": {
-		"vite": "^2.0.0"
+		"vite": "^3.0.0"
 	},
 	"pnpm": {
 		"overrides": {
diff --git a/packages/create-svelte/templates/default/package.json b/packages/create-svelte/templates/default/package.json
index bf0689242..ce127a154 100644
--- a/packages/create-svelte/templates/default/package.json
+++ b/packages/create-svelte/templates/default/package.json
@@ -13,7 +13,7 @@
 		"svelte": "^3.48.0",
 		"svelte-preprocess": "^4.10.6",
 		"typescript": "^4.7.4",
-		"vite": "^3.0.0"
+		"vite": "^3.0.4"
 	},
 	"type": "module",
 	"dependencies": {
diff --git a/packages/create-svelte/templates/default/package.template.json b/packages/create-svelte/templates/default/package.template.json
index 3e51b6a62..d4cc74df2 100644
--- a/packages/create-svelte/templates/default/package.template.json
+++ b/packages/create-svelte/templates/default/package.template.json
@@ -11,7 +11,7 @@
 		"@sveltejs/adapter-auto": "next",
 		"@sveltejs/kit": "next",
 		"svelte": "^3.46.0",
-		"vite": "^3.0.0"
+		"vite": "^3.0.4"
 	},
 	"type": "module",
 	"dependencies": {
diff --git a/packages/create-svelte/templates/skeleton/package.template.json b/packages/create-svelte/templates/skeleton/package.template.json
index 9b213b5b1..0d2623c4e 100644
--- a/packages/create-svelte/templates/skeleton/package.template.json
+++ b/packages/create-svelte/templates/skeleton/package.template.json
@@ -11,7 +11,7 @@
 		"@sveltejs/adapter-auto": "workspace:*",
 		"@sveltejs/kit": "workspace:*",
 		"svelte": "^3.44.0",
-		"vite": "^3.0.0"
+		"vite": "^3.0.4"
 	},
 	"type": "module"
 }
diff --git a/packages/kit/package.json b/packages/kit/package.json
index ce4d052af..f6ce39789 100644
--- a/packages/kit/package.json
+++ b/packages/kit/package.json
@@ -11,7 +11,6 @@
 	"type": "module",
 	"dependencies": {
 		"@sveltejs/vite-plugin-svelte": "^1.0.1",
-		"chokidar": "^3.5.3",
 		"cookie": "^0.5.0",
 		"devalue": "^2.0.1",
 		"kleur": "^4.1.4",
diff --git a/packages/kit/src/runtime/server/page/index.js b/packages/kit/src/runtime/server/page/index.js
index 9840a417c..9a1769bb0 100644
--- a/packages/kit/src/runtime/server/page/index.js
+++ b/packages/kit/src/runtime/server/page/index.js
@@ -77,7 +77,7 @@ export async function render_page(event, route, options, state, resolve_opts) {
 					}
 
 					if (event.request.method === 'POST' && result?.location) {
-						return redirect_response(status, result.location);
+						return redirect_response(303, result.location);
 					}
 				} else {
 					event.setHeaders({
diff --git a/packages/kit/src/runtime/server/page/render.js b/packages/kit/src/runtime/server/page/render.js
index 31d9f494a..019229d13 100644
--- a/packages/kit/src/runtime/server/page/render.js
+++ b/packages/kit/src/runtime/server/page/render.js
@@ -150,9 +150,31 @@ export async function render_response({
 
 	const target = hash(body);
 
+	/**
+	 * The prefix to use for static assets. Replaces `%sveltekit.assets%` in the template
+	 * @type {string}
+	 */
+	let assets;
+
+	if (options.paths.assets) {
+		// if an asset path is specified, use it
+		assets = options.paths.assets;
+	} else if (state.prerendering?.fallback) {
+		// if we're creating a fallback page, asset paths need to be root-relative
+		assets = options.paths.base;
+	} else {
+		// otherwise we want asset paths to be relative to the page, so that they
+		// will work in odd contexts like IPFS, the internet archive, and so on
+		const segments = event.url.pathname.slice(options.paths.base.length).split('/').slice(2);
+		assets = segments.length > 0 ? segments.map(() => '..').join('/') : '.';
+	}
+
+	/** @param {string} path */
+	const prefixed = (path) => (path.startsWith('/') ? path : `${assets}/${path}`);
+
 	// prettier-ignore
 	const init_app = `
-		import { set_public_env, start } from ${s(options.prefix + entry.file)};
+		import { set_public_env, start } from ${s(prefixed(entry.file))};
 
 		set_public_env(${s(options.public_env)});
 
@@ -195,7 +217,7 @@ export async function render_response({
 	}
 
 	for (const dep of stylesheets) {
-		const path = options.prefix + dep;
+		const path = prefixed(dep);
 		const attributes = [];
 
 		if (csp.style_needs_nonce) {
@@ -217,7 +239,7 @@ export async function render_response({
 
 	if (page_config.router || page_config.hydrate) {
 		for (const dep of modulepreloads) {
-			const path = options.prefix + dep;
+			const path = prefixed(dep);
 			link_header_preloads.add(`<${encodeURI(path)}>; rel="modulepreload"; nopush`);
 			if (state.prerendering) {
 				head += `\n\t<link rel="modulepreload" href="${path}">`;
@@ -292,10 +314,6 @@ export async function render_response({
 		}
 	}
 
-	const segments = event.url.pathname.slice(options.paths.base.length).split('/').slice(2);
-	const assets =
-		options.paths.assets || (segments.length > 0 ? segments.map(() => '..').join('/') : '.');
-
 	// TODO flush chunks as early as we can
 	const html =
 		(await resolve_opts.transformPageChunk({
diff --git a/packages/kit/src/vite/build/build_server.js b/packages/kit/src/vite/build/build_server.js
index e0a778c5f..7414463b8 100644
--- a/packages/kit/src/vite/build/build_server.js
+++ b/packages/kit/src/vite/build/build_server.js
@@ -69,7 +69,6 @@ export class Server {
 			manifest,
 			method_override: ${s(config.kit.methodOverride)},
 			paths: { base, assets },
-			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 35cdc330a..a1b3da9f8 100644
--- a/packages/kit/src/vite/build/utils.js
+++ b/packages/kit/src/vite/build/utils.js
@@ -88,9 +88,11 @@ export function find_deps(manifest, entry, add_dynamic_css) {
  * @return {import('vite').UserConfig}
  */
 export const get_default_config = function ({ config, input, ssr, outDir }) {
+	const prefix = `${config.kit.appDir}/immutable`;
+
 	return {
 		appType: 'custom',
-		base: assets_base(config.kit),
+		base: ssr ? assets_base(config.kit) : './',
 		build: {
 			cssCodeSplit: true,
 			// don't use the default name to avoid collisions with 'static/manifest.json'
@@ -101,11 +103,9 @@ export const get_default_config = function ({ config, input, ssr, outDir }) {
 				input,
 				output: {
 					format: 'esm',
-					entryFileNames: ssr ? '[name].js' : `${config.kit.appDir}/immutable/[name]-[hash].js`,
-					chunkFileNames: ssr
-						? 'chunks/[name].js'
-						: `${config.kit.appDir}/immutable/chunks/[name]-[hash].js`,
-					assetFileNames: `${config.kit.appDir}/immutable/assets/[name]-[hash][extname]`
+					entryFileNames: ssr ? '[name].js' : `${prefix}/[name]-[hash].js`,
+					chunkFileNames: ssr ? 'chunks/[name].js' : `${prefix}/chunks/[name]-[hash].js`,
+					assetFileNames: `${prefix}/assets/[name]-[hash][extname]`
 				},
 				preserveEntrySignatures: 'strict'
 			},
@@ -125,6 +125,14 @@ export const get_default_config = function ({ config, input, ssr, outDir }) {
 		},
 		ssr: {
 			noExternal: ['@sveltejs/kit']
+		},
+		worker: {
+			rollupOptions: {
+				output: {
+					entryFileNames: `${prefix}/workers/[name]-[hash].js`,
+					chunkFileNames: `${prefix}/workers/chunks/[name]-[hash].js`
+				}
+			}
 		}
 	};
 };
@@ -134,11 +142,7 @@ export const get_default_config = function ({ config, input, ssr, outDir }) {
  * @returns {string}
  */
 export function assets_base(config) {
-	// TODO this is so that Vite's preloading works. Unfortunately, it fails
-	// 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}/`;
+	return config.paths.assets || config.paths.base || './';
 }
 
 const method_names = new Set(['GET', 'HEAD', 'PUT', 'POST', 'DELETE', 'PATCH']);
diff --git a/packages/kit/src/vite/dev/index.js b/packages/kit/src/vite/dev/index.js
index 0c20b7a91..c67f11fe2 100644
--- a/packages/kit/src/vite/dev/index.js
+++ b/packages/kit/src/vite/dev/index.js
@@ -432,7 +432,6 @@ export async function dev(vite, vite_config, svelte_config, illegal_imports) {
 							base: svelte_config.kit.paths.base,
 							assets
 						},
-						prefix: '',
 						prerender: {
 							default: svelte_config.kit.prerender.default,
 							enabled: svelte_config.kit.prerender.enabled
diff --git a/packages/kit/src/vite/index.js b/packages/kit/src/vite/index.js
index 70a9cf482..a5ef0f32e 100644
--- a/packages/kit/src/vite/index.js
+++ b/packages/kit/src/vite/index.js
@@ -207,7 +207,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/`
+				client_out_dir: `${svelte_config.kit.outDir}/output/client`
 			};
 
 			illegal_imports = new Set([
@@ -230,7 +230,7 @@ function kit() {
 			/** @type {import('vite').UserConfig} */
 			const result = {
 				appType: 'custom',
-				base: '/',
+				base: './',
 				build: {
 					rollupOptions: {
 						// Vite dependency crawler needs an explicit JS entry point
diff --git a/packages/kit/types/internal.d.ts b/packages/kit/types/internal.d.ts
index a9938d281..9738ab5a0 100644
--- a/packages/kit/types/internal.d.ts
+++ b/packages/kit/types/internal.d.ts
@@ -239,7 +239,6 @@ export interface SSROptions {
 		base: string;
 		assets: string;
 	};
-	prefix: string;
 	prerender: {
 		default: boolean;
 		enabled: boolean;
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index d13023cd9..3b958fc87 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -247,7 +247,7 @@ importers:
       svelte: ^3.48.0
       svelte-preprocess: ^4.10.6
       typescript: ^4.7.4
-      vite: ^3.0.0
+      vite: ^3.0.4
     dependencies:
       '@fontsource/fira-mono': 4.5.8
       '@lukeed/uuid': 2.0.0
@@ -277,7 +277,6 @@ importers:
       '@types/node': ^16.11.36
       '@types/sade': ^1.7.4
       '@types/set-cookie-parser': ^2.4.2
-      chokidar: ^3.5.3
       cookie: ^0.5.0
       devalue: ^2.0.1
       kleur: ^4.1.4
@@ -298,7 +297,6 @@ importers:
       vite: 3.0.2
     dependencies:
       '@sveltejs/vite-plugin-svelte': 1.0.1_svelte@3.48.0+vite@3.0.2
-      chokidar: 3.5.3
       cookie: 0.5.0
       devalue: 2.0.1
       kleur: 4.1.5
@@ -556,7 +554,7 @@ importers:
       shiki-twoslash: ^3.0.2
       svelte: ^3.48.0
       typescript: ^4.7.4
-      vite: ^3.0.0
+      vite: ^3.0.4
       vite-imagetools: ^4.0.3
     devDependencies:
       '@sveltejs/adapter-auto': link:../../packages/adapter-auto
diff --git a/sites/kit.svelte.dev/package.json b/sites/kit.svelte.dev/package.json
index 616f17709..5be7b5e44 100644
--- a/sites/kit.svelte.dev/package.json
+++ b/sites/kit.svelte.dev/package.json
@@ -21,7 +21,7 @@
 		"shiki-twoslash": "^3.0.2",
 		"svelte": "^3.48.0",
 		"typescript": "^4.7.4",
-		"vite": "^3.0.0",
+		"vite": "^3.0.4",
 		"vite-imagetools": "^4.0.3"
 	},
 	"type": "module"


Test patch:
diff --git a/packages/adapter-static/test/apps/spa/src/routes/fallback/[...rest]/+page.svelte b/packages/adapter-static/test/apps/spa/src/routes/fallback/[...rest]/+page.svelte
new file mode 100644
index 000000000..671b43ec7
--- /dev/null
+++ b/packages/adapter-static/test/apps/spa/src/routes/fallback/[...rest]/+page.svelte
@@ -0,0 +1,1 @@
+<h1>the fallback page was rendered</h1>
diff --git a/packages/adapter-static/test/test.js b/packages/adapter-static/test/test.js
index ffc1f4072..97bef6e5d 100644
--- a/packages/adapter-static/test/test.js
+++ b/packages/adapter-static/test/test.js
@@ -14,8 +14,11 @@ run('prerendered', (test) => {
 });
 
 run('spa', (test) => {
-	test('generates a fallback page', ({ cwd }) => {
+	test('generates a fallback page', async ({ base, cwd, page }) => {
 		assert.ok(fs.existsSync(`${cwd}/build/200.html`));
+
+		await page.goto(`${base}/fallback/a/b/c`);
+		assert.equal(await page.textContent('h1'), 'the fallback page was rendered');
 	});
 
 	test('does not prerender pages without prerender=true', ({ cwd }) => {
diff --git a/packages/kit/test/apps/basics/src/routes/shadowed/post-success-redirect/+page.server.js b/packages/kit/test/apps/basics/src/routes/shadowed/post-success-redirect/+page.server.js
new file mode 100644
index 000000000..30d0b0e3f
--- /dev/null
+++ b/packages/kit/test/apps/basics/src/routes/shadowed/post-success-redirect/+page.server.js
@@ -0,0 +1,5 @@
+export function POST() {
+	return {
+		location: '/shadowed/post-success-redirect/redirected'
+	};
+}
diff --git a/packages/kit/test/apps/basics/src/routes/shadowed/post-success-redirect/+page.svelte b/packages/kit/test/apps/basics/src/routes/shadowed/post-success-redirect/+page.svelte
new file mode 100644
index 000000000..74dca32e8
--- /dev/null
+++ b/packages/kit/test/apps/basics/src/routes/shadowed/post-success-redirect/+page.svelte
@@ -0,0 +1,3 @@
+<form method="POST">
+	<button>Click me</button>
+</form>
diff --git a/packages/kit/test/apps/basics/src/routes/shadowed/post-success-redirect/redirected/+page.svelte b/packages/kit/test/apps/basics/src/routes/shadowed/post-success-redirect/redirected/+page.svelte
new file mode 100644
index 000000000..6ba792f15
--- /dev/null
+++ b/packages/kit/test/apps/basics/src/routes/shadowed/post-success-redirect/redirected/+page.svelte
@@ -0,0 +1,1 @@
+<h1>POST was successful</h1>
diff --git a/packages/kit/test/apps/basics/test/test.js b/packages/kit/test/apps/basics/test/test.js
index d03fa9600..e54c9ec49 100644
--- a/packages/kit/test/apps/basics/test/test.js
+++ b/packages/kit/test/apps/basics/test/test.js
@@ -188,6 +188,12 @@ test.describe('Shadowed pages', () => {
 		);
 	});
 
+	test('Handles POST success with returned location', async ({ page }) => {
+		await page.goto('/shadowed/post-success-redirect');
+		await Promise.all([page.waitForNavigation(), page.click('button')]);
+		expect(await page.textContent('h1')).toBe('POST was successful');
+	});
+
 	test('Renders error page for 4xx and 5xx responses from GET', async ({ page, clicknav }) => {
 		await page.goto('/shadowed');
 		await clicknav('[href="/shadowed/error-get"]');