Skip to content
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/tiny-news-clap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@sveltejs/kit": patch
---

fix: handle `onNavigate` callbacks correctly
6 changes: 2 additions & 4 deletions packages/kit/src/runtime/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -1330,7 +1330,7 @@ async function navigate({
fn(/** @type {import('@sveltejs/kit').OnNavigate} */ (nav.navigation))
)
)
).filter((value) => typeof value === 'function');
).filter(/** @returns {value is () => void} */ (value) => typeof value === 'function');

if (after_navigate.length > 0) {
function cleanup() {
Expand All @@ -1341,9 +1341,7 @@ async function navigate({
}

after_navigate.push(cleanup);

// @ts-ignore
callbacks.after_navigate.push(...after_navigate);
after_navigate_callbacks.push(...after_navigate);
}

root.$set(navigation_result.props);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,20 @@
/** @type {Omit<import('@sveltejs/kit').NavigationType, 'enter' | 'leave'>} */
let type;

let called_return = false;

onNavigate((navigation) => {
from = navigation.from;
to = navigation.to;
type = navigation.type;
});

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

<h1>{from?.url.pathname} -> {to?.url.pathname} ({type ?? '...'})</h1>
<h1>{from?.url.pathname} -> {to?.url.pathname} ({type ?? '...'}) {called_return}</h1>
<a href="/navigation-lifecycle/on-navigate/b">/b</a>
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,11 @@ test.describe('Navigation lifecycle functions', () => {

test('onNavigate calls callback', async ({ page, clicknav }) => {
await page.goto('/navigation-lifecycle/on-navigate/a');
expect(await page.textContent('h1')).toBe('undefined -> undefined (...)');
expect(await page.textContent('h1')).toBe('undefined -> undefined (...) false');

await clicknav('[href="/navigation-lifecycle/on-navigate/b"]');
expect(await page.textContent('h1')).toBe(
'/navigation-lifecycle/on-navigate/a -> /navigation-lifecycle/on-navigate/b (link)'
'/navigation-lifecycle/on-navigate/a -> /navigation-lifecycle/on-navigate/b (link) true'
);
});
});
Expand Down