Skip to content

Commit

Permalink
fix: runtime error when onNavigate returns a function (#11678)
Browse files Browse the repository at this point in the history
Fixes #11600
  • Loading branch information
f-elix authored Jan 19, 2024
1 parent 35a9fd9 commit e228f89
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
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

0 comments on commit e228f89

Please sign in to comment.