Skip to content

Commit e228f89

Browse files
authored
fix: runtime error when onNavigate returns a function (#11678)
Fixes #11600
1 parent 35a9fd9 commit e228f89

File tree

4 files changed

+18
-7
lines changed

4 files changed

+18
-7
lines changed

.changeset/tiny-news-clap.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@sveltejs/kit": patch
3+
---
4+
5+
fix: handle `onNavigate` callbacks correctly

packages/kit/src/runtime/client/client.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,7 +1330,7 @@ async function navigate({
13301330
fn(/** @type {import('@sveltejs/kit').OnNavigate} */ (nav.navigation))
13311331
)
13321332
)
1333-
).filter((value) => typeof value === 'function');
1333+
).filter(/** @returns {value is () => void} */ (value) => typeof value === 'function');
13341334

13351335
if (after_navigate.length > 0) {
13361336
function cleanup() {
@@ -1341,9 +1341,7 @@ async function navigate({
13411341
}
13421342

13431343
after_navigate.push(cleanup);
1344-
1345-
// @ts-ignore
1346-
callbacks.after_navigate.push(...after_navigate);
1344+
after_navigate_callbacks.push(...after_navigate);
13471345
}
13481346

13491347
root.$set(navigation_result.props);

packages/kit/test/apps/basics/src/routes/navigation-lifecycle/on-navigate/[x]/+page.svelte

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,20 @@
1010
/** @type {Omit<import('@sveltejs/kit').NavigationType, 'enter' | 'leave'>} */
1111
let type;
1212
13+
let called_return = false;
14+
1315
onNavigate((navigation) => {
1416
from = navigation.from;
1517
to = navigation.to;
1618
type = navigation.type;
1719
});
20+
21+
onNavigate(() => {
22+
return () => {
23+
called_return = true;
24+
};
25+
});
1826
</script>
1927

20-
<h1>{from?.url.pathname} -> {to?.url.pathname} ({type ?? '...'})</h1>
28+
<h1>{from?.url.pathname} -> {to?.url.pathname} ({type ?? '...'}) {called_return}</h1>
2129
<a href="/navigation-lifecycle/on-navigate/b">/b</a>

packages/kit/test/apps/basics/test/cross-platform/client.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,11 +240,11 @@ test.describe('Navigation lifecycle functions', () => {
240240

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

245245
await clicknav('[href="/navigation-lifecycle/on-navigate/b"]');
246246
expect(await page.textContent('h1')).toBe(
247-
'/navigation-lifecycle/on-navigate/a -> /navigation-lifecycle/on-navigate/b (link)'
247+
'/navigation-lifecycle/on-navigate/a -> /navigation-lifecycle/on-navigate/b (link) true'
248248
);
249249
});
250250
});

0 commit comments

Comments
 (0)