Skip to content

fix: correctly remove navigation callbacks when returning function in onNavigate #13241

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/sixty-rabbits-smell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: correctly remove navigation callbacks when returning function in onNavigate
39 changes: 22 additions & 17 deletions packages/kit/src/runtime/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,14 +191,18 @@ const components = [];
/** @type {{id: string, token: {}, promise: Promise<import('./types.js').NavigationResult>} | null} */
let load_cache = null;

/** @type {Array<(navigation: import('@sveltejs/kit').BeforeNavigate) => void>} */
const before_navigate_callbacks = [];
/**
* Note on before_navigate_callbacks, on_navigate_callbacks and after_navigate_callbacks:
* do not re-assign as some closures keep references to these Sets
*/
/** @type {Set<(navigation: import('@sveltejs/kit').BeforeNavigate) => void>} */
const before_navigate_callbacks = new Set();

/** @type {Array<(navigation: import('@sveltejs/kit').OnNavigate) => import('types').MaybePromise<(() => void) | void>>} */
const on_navigate_callbacks = [];
/** @type {Set<(navigation: import('@sveltejs/kit').OnNavigate) => import('types').MaybePromise<(() => void) | void>>} */
const on_navigate_callbacks = new Set();

/** @type {Array<(navigation: import('@sveltejs/kit').AfterNavigate) => void>} */
let after_navigate_callbacks = [];
/** @type {Set<(navigation: import('@sveltejs/kit').AfterNavigate) => void>} */
const after_navigate_callbacks = new Set();

/** @type {import('./types.js').NavigationState} */
let current = {
Expand Down Expand Up @@ -1460,22 +1464,24 @@ async function navigate({

const after_navigate = (
await Promise.all(
on_navigate_callbacks.map((fn) =>
Array.from(on_navigate_callbacks, (fn) =>
fn(/** @type {import('@sveltejs/kit').OnNavigate} */ (nav.navigation))
)
)
).filter(/** @returns {value is () => void} */ (value) => typeof value === 'function');

if (after_navigate.length > 0) {
function cleanup() {
after_navigate_callbacks = after_navigate_callbacks.filter(
// @ts-ignore
(fn) => !after_navigate.includes(fn)
);
after_navigate.forEach((fn) => {
after_navigate_callbacks.delete(fn);
});
}

after_navigate.push(cleanup);
after_navigate_callbacks.push(...after_navigate);

after_navigate.forEach((fn) => {
after_navigate_callbacks.add(fn);
});
}

root.$set(navigation_result.props);
Expand Down Expand Up @@ -1677,7 +1683,7 @@ function setup_preload() {
}
}

after_navigate_callbacks.push(after_navigate);
after_navigate_callbacks.add(after_navigate);
after_navigate();
}

Expand Down Expand Up @@ -1706,16 +1712,15 @@ function handle_error(error, event) {

/**
* @template {Function} T
* @param {T[]} callbacks
* @param {Set<T>} callbacks
* @param {T} callback
*/
function add_navigation_callback(callbacks, callback) {
onMount(() => {
callbacks.push(callback);
callbacks.add(callback);

return () => {
const i = callbacks.indexOf(callback);
callbacks.splice(i, 1);
callbacks.delete(callback);
};
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<script>
import { onNavigate } from '$app/navigation';
let { children } = $props();

onNavigate(() => {
return () => {};
});
</script>

{@render children()}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<script>
import { afterNavigate } from '$app/navigation';

afterNavigate(() => {
console.log('after navigate called');

/**
* @type {HTMLElement}
*/
const el = document.querySelector('.nav-lifecycle-after-nav-removed-test-target');

if (el) {
el.innerText = 'true';
}
});
</script>

<h1>/A</h1>
<a href="/navigation-lifecycle/after-navigate-properly-removed/a">/a</a>
<a href="/navigation-lifecycle/after-navigate-properly-removed/b">/b</a>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<h1>/B</h1>
<div>
was called:
<span class="nav-lifecycle-after-nav-removed-test-target">false</span>
</div>
<a href="/navigation-lifecycle/after-navigate-properly-removed/a">/a</a>
<a href="/navigation-lifecycle/after-navigate-properly-removed/b">/b</a>
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
test.describe.configure({ mode: 'parallel' });

test.describe('a11y', () => {
test('resets focus', async ({ page, clicknav, browserName }) => {

Check warning on line 12 in packages/kit/test/apps/basics/test/cross-platform/client.test.js

View workflow job for this annotation

GitHub Actions / test-kit-cross-browser (18, windows-2019, chromium, dev)

flaky test: resets focus

retries: 2
const tab = browserName === 'webkit' ? 'Alt+Tab' : 'Tab';

await page.goto('/accessibility/a');
Expand All @@ -33,7 +33,7 @@
expect(await page.evaluate(() => document.documentElement.getAttribute('tabindex'))).toBe(null);
});

test('applies autofocus after a navigation', async ({ page, clicknav }) => {

Check warning on line 36 in packages/kit/test/apps/basics/test/cross-platform/client.test.js

View workflow job for this annotation

GitHub Actions / test-kit-cross-browser (18, windows-2019, chromium, dev)

flaky test: applies autofocus after a navigation

retries: 2
await page.goto('/accessibility/autofocus/a');

await clicknav('[href="/accessibility/autofocus/b"]');
Expand Down Expand Up @@ -256,6 +256,14 @@
'/navigation-lifecycle/on-navigate/a -> /navigation-lifecycle/on-navigate/b (link) true'
);
});

test('afterNavigate properly removed', async ({ page, clicknav }) => {
await page.goto('/navigation-lifecycle/after-navigate-properly-removed/b');
await clicknav('[href="/navigation-lifecycle/after-navigate-properly-removed/a"]');
await clicknav('[href="/navigation-lifecycle/after-navigate-properly-removed/b"]');

expect(await page.textContent('.nav-lifecycle-after-nav-removed-test-target')).toBe('false');
});
});

test.describe('Scrolling', () => {
Expand Down
Loading