Repository: sveltejs/kit
Pull request #7594 (2022-11-10 18:06:51)

remove --force from test script
I know we've run into sticky caches before, but at the moment we're not using Turborepo at all. For things like docs changes we're causing CI to take waaaay longer than it needs to. I think we should remove `--force` and deal with caching bugs as they arise

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

Latest commit: 18424b5edb65fea76f03574cf8250465a80f313e

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/sveltejs/kit/new/enable-caches?filename=.changeset/three-snails-fetch.md&value=---%0A%0A---%0A%0Aremove%20--force%20from%20test%20script%0A)



Patch:
diff --git a/.changeset/hungry-tips-obey.md b/.changeset/hungry-tips-obey.md
new file mode 100644
index 000000000..f59b225de
--- /dev/null
+++ b/.changeset/hungry-tips-obey.md
@@ -0,0 +1,5 @@
+---
+'@sveltejs/kit': patch
+---
+
+[breaking] don't run beforeNavigate during redirects
diff --git a/.changeset/pre.json b/.changeset/pre.json
index 9bc0b1ec6..361f52738 100644
--- a/.changeset/pre.json
+++ b/.changeset/pre.json
@@ -589,6 +589,7 @@
     "large-kangaroos-remain",
     "large-lies-reply",
     "large-seahorses-check",
+    "large-starfishes-relate",
     "late-bats-rule",
     "late-bobcats-brake",
     "late-cats-care",
@@ -972,6 +973,7 @@
     "rich-walls-doubt",
     "rotten-cats-pump",
     "rotten-cherries-jump",
+    "rotten-files-work",
     "rotten-gorillas-sort",
     "rotten-mice-compete",
     "rotten-rocks-mate",
diff --git a/package.json b/package.json
index 3d7f72981..2c9b245d0 100644
--- a/package.json
+++ b/package.json
@@ -4,7 +4,7 @@
 	"description": "monorepo for @sveltejs/kit and friends",
 	"private": true,
 	"scripts": {
-		"test": "turbo run test --filter=./packages/* --force",
+		"test": "turbo run test --filter=./packages/*",
 		"check": "turbo run check",
 		"lint": "turbo run lint",
 		"format": "turbo run format",
diff --git a/packages/kit/CHANGELOG.md b/packages/kit/CHANGELOG.md
index 9005b806f..f2f0050fc 100644
--- a/packages/kit/CHANGELOG.md
+++ b/packages/kit/CHANGELOG.md
@@ -1,5 +1,15 @@
 # @sveltejs/kit
 
+## 1.0.0-next.541
+
+### Minor Changes
+
+- [fix] don't clean build and output directories when using `--watch` ([#7516](https://github.com/sveltejs/kit/pull/7516))
+
+### Patch Changes
+
+- [fix] handle expected errors thrown in handle hook correctly ([#7566](https://github.com/sveltejs/kit/pull/7566))
+
 ## 1.0.0-next.540
 
 ### Patch Changes
diff --git a/packages/kit/package.json b/packages/kit/package.json
index c9fae0d78..611fef465 100644
--- a/packages/kit/package.json
+++ b/packages/kit/package.json
@@ -1,6 +1,6 @@
 {
 	"name": "@sveltejs/kit",
-	"version": "1.0.0-next.540",
+	"version": "1.0.0-next.541",
 	"repository": {
 		"type": "git",
 		"url": "https://github.com/sveltejs/kit",
diff --git a/packages/kit/src/runtime/client/client.js b/packages/kit/src/runtime/client/client.js
index e871bbf39..ed26e5440 100644
--- a/packages/kit/src/runtime/client/client.js
+++ b/packages/kit/src/runtime/client/client.js
@@ -1066,7 +1066,10 @@ export function create_client({ target, base, trailing_slash }) {
 			}
 		};
 
-		callbacks.before_navigate.forEach((fn) => fn(cancellable));
+		if (!navigating) {
+			// Don't run the event during redirects
+			callbacks.before_navigate.forEach((fn) => fn(cancellable));
+		}
 
 		return should_block ? null : navigation;
 	}
diff --git a/sites/kit.svelte.dev/.gitignore b/sites/kit.svelte.dev/.gitignore
index bf50b6d41..152177efd 100644
--- a/sites/kit.svelte.dev/.gitignore
+++ b/sites/kit.svelte.dev/.gitignore
@@ -1,5 +1,6 @@
 .DS_Store
 .env
 /.svelte-kit/
+/.snippets
 /build/
-/functions/
+/functions/
\ No newline at end of file
diff --git a/sites/kit.svelte.dev/src/lib/docs/server/index.js b/sites/kit.svelte.dev/src/lib/docs/server/index.js
index cc25ea840..a42752773 100644
--- a/sites/kit.svelte.dev/src/lib/docs/server/index.js
+++ b/sites/kit.svelte.dev/src/lib/docs/server/index.js
@@ -12,6 +12,13 @@ import { render_modules } from './modules.js';
 import { parse_route_id } from '../../../../../../packages/kit/src/utils/routing.js';
 import ts from 'typescript';
 import MagicString from 'magic-string';
+import { fileURLToPath } from 'url';
+import { createHash } from 'crypto';
+
+const snippet_cache = fileURLToPath(new URL('../../../../.snippets', import.meta.url));
+if (!fs.existsSync(snippet_cache)) {
+	fs.mkdirSync(snippet_cache, { recursive: true });
+}
 
 const languages = {
 	bash: 'bash',
@@ -54,8 +61,6 @@ export async function read_file(file) {
 	const match = /\d{2}-(.+)\.md/.exec(file.split(path.sep).pop());
 	if (!match) return null;
 
-	const slug = match[1];
-
 	const markdown = fs
 		.readFileSync(`${base}/${file}`, 'utf-8')
 		.replace('**TYPES**', () => render_modules('types'))
@@ -68,8 +73,15 @@ export async function read_file(file) {
 	const { content, sections } = parse({
 		body: generate_ts_from_js(body),
 		file,
-		slug,
 		code: (source, language, current) => {
+			const hash = createHash('sha256');
+			hash.update(source + language + current);
+			const digest = hash.digest().toString('base64').replace(/\//g, '-');
+
+			if (fs.existsSync(`${snippet_cache}/${digest}.html`)) {
+				return fs.readFileSync(`${snippet_cache}/${digest}.html`, 'utf-8');
+			}
+
 			/** @type {Record<string, string>} */
 			const options = {};
 
@@ -213,7 +225,7 @@ export async function read_file(file) {
 
 			type_regex.lastIndex = 0;
 
-			return html
+			html = html
 				.replace(type_regex, (match, prefix, name) => {
 					if (options.link === 'false' || name === current) {
 						// we don't want e.g. RequestHandler to link to RequestHandler
@@ -240,6 +252,9 @@ export async function read_file(file) {
 							.join('');
 					}
 				);
+
+			fs.writeFileSync(`${snippet_cache}/${digest}.html`, html);
+			return html;
 		},
 		codespan: (text) => {
 			return (
@@ -266,12 +281,11 @@ export async function read_file(file) {
  * @param {{
  *   body: string;
  *   file: string;
- *   slug: string;
  *   code: (source: string, language: string, current: string) => string;
  *   codespan: (source: string) => string;
  * }} opts
  */
-function parse({ body, file, slug, code, codespan }) {
+function parse({ body, file, code, codespan }) {
 	const headings = [];
 
 	/** @type {import('./types').Section[]} */


Test patch:
diff --git a/packages/kit/test/apps/basics/src/routes/before-navigate/prevent-navigation/+page.svelte b/packages/kit/test/apps/basics/src/routes/before-navigate/prevent-navigation/+page.svelte
index 829056d1f..402bc60cf 100644
--- a/packages/kit/test/apps/basics/src/routes/before-navigate/prevent-navigation/+page.svelte
+++ b/packages/kit/test/apps/basics/src/routes/before-navigate/prevent-navigation/+page.svelte
@@ -1,18 +1,21 @@
 <script>
 	import { beforeNavigate } from '$app/navigation';
 
-	let triggered = false;
+	let times_triggered = 0;
 	let unload = false;
 	let navigation_type;
-	beforeNavigate(({ cancel, type, willUnload }) => {
-		triggered = true;
+	beforeNavigate(({ cancel, type, willUnload, to }) => {
+		times_triggered++;
 		unload = willUnload;
 		navigation_type = type;
-		cancel();
+		if (!to?.route.id?.includes('redirect')) {
+			cancel();
+		}
 	});
 </script>
 
 <h1>prevent navigation</h1>
 <a href="/before-navigate/a">a</a>
+<a href="/before-navigate/redirect">redirect</a>
 <a href="https://google.de">external</a>
-<pre>{triggered} {unload} {navigation_type}</pre>
+<pre>{times_triggered} {unload} {navigation_type}</pre>
diff --git a/packages/kit/test/apps/basics/src/routes/before-navigate/redirect/+page.js b/packages/kit/test/apps/basics/src/routes/before-navigate/redirect/+page.js
new file mode 100644
index 000000000..a5b9dcc34
--- /dev/null
+++ b/packages/kit/test/apps/basics/src/routes/before-navigate/redirect/+page.js
@@ -0,0 +1,5 @@
+import { redirect } from '@sveltejs/kit';
+
+export function load() {
+	throw redirect(307, '/before-navigate/prevent-navigation');
+}
diff --git a/packages/kit/test/apps/basics/test/client.test.js b/packages/kit/test/apps/basics/test/client.test.js
index 8a04dd861..1d8e48f2b 100644
--- a/packages/kit/test/apps/basics/test/client.test.js
+++ b/packages/kit/test/apps/basics/test/client.test.js
@@ -27,7 +27,7 @@ test.describe('beforeNavigate', () => {
 		await page.waitForLoadState('networkidle');
 
 		expect(page.url()).toBe(baseURL + '/before-navigate/prevent-navigation');
-		expect(await page.innerHTML('pre')).toBe('true false link');
+		expect(await page.innerHTML('pre')).toBe('1 false link');
 	});
 
 	test('prevents navigation to external', async ({ page, baseURL }) => {
@@ -38,7 +38,7 @@ test.describe('beforeNavigate', () => {
 
 		page.click('a[href="https://google.de"]'); // do NOT await this, promise only resolves after successful navigation, which never happens
 		await page.waitForTimeout(500);
-		await expect(page.locator('pre')).toHaveText('true true link');
+		await expect(page.locator('pre')).toHaveText('1 true link');
 		expect(page.url()).toBe(baseURL + '/before-navigate/prevent-navigation');
 	});
 
@@ -46,14 +46,14 @@ test.describe('beforeNavigate', () => {
 		await page.goto('/before-navigate/prevent-navigation');
 		await app.goto('/before-navigate/a');
 		expect(page.url()).toBe(baseURL + '/before-navigate/prevent-navigation');
-		expect(await page.innerHTML('pre')).toBe('true false goto');
+		expect(await page.innerHTML('pre')).toBe('1 false goto');
 	});
 
 	test('prevents external navigation triggered by goto', async ({ page, app, baseURL }) => {
 		await page.goto('/before-navigate/prevent-navigation');
 		await app.goto('https://google.de');
 		expect(page.url()).toBe(baseURL + '/before-navigate/prevent-navigation');
-		expect(await page.innerHTML('pre')).toBe('true true goto');
+		expect(await page.innerHTML('pre')).toBe('1 true goto');
 	});
 
 	test('prevents navigation triggered by back button', async ({ page, app, baseURL }) => {
@@ -62,7 +62,7 @@ test.describe('beforeNavigate', () => {
 		await page.click('h1'); // The browsers block attempts to prevent navigation on a frame that's never had a user gesture.
 
 		await page.goBack();
-		expect(await page.innerHTML('pre')).toBe('true false popstate');
+		expect(await page.innerHTML('pre')).toBe('1 false popstate');
 		expect(page.url()).toBe(baseURL + '/before-navigate/prevent-navigation');
 	});
 
@@ -78,7 +78,17 @@ test.describe('beforeNavigate', () => {
 
 		await page.close({ runBeforeUnload: true });
 		expect(await type).toBe('beforeunload');
-		expect(await page.innerHTML('pre')).toBe('true true leave');
+		expect(await page.innerHTML('pre')).toBe('1 true leave');
+	});
+
+	test('is not triggered on redirect', async ({ page, baseURL }) => {
+		await page.goto('/before-navigate/prevent-navigation');
+
+		await page.click('[href="/before-navigate/redirect"]');
+		await page.waitForLoadState('networkidle');
+
+		expect(page.url()).toBe(baseURL + '/before-navigate/prevent-navigation');
+		expect(await page.innerHTML('pre')).toBe('1 false link');
 	});
 });