Skip to content

Commit 8d38e5a

Browse files
fix: fulfil beforeNavigate complete when redirected (#12896)
* add fix * add test * Update .changeset/gold-horses-return.md * fulfil promise after redirect has occurred --------- Co-authored-by: Rich Harris <[email protected]>
1 parent 181f8f7 commit 8d38e5a

File tree

4 files changed

+29
-3
lines changed

4 files changed

+29
-3
lines changed

.changeset/gold-horses-return.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: fulfil `beforeNavigate` `complete` when redirected

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ export async function _goto(url, options, redirect_count, nav_token) {
461461
load_cache = null;
462462
}
463463

464-
const result = await navigate({
464+
await navigate({
465465
type: 'goto',
466466
url: resolve_url(url),
467467
keepfocus: options.keepFocus,
@@ -481,6 +481,7 @@ export async function _goto(url, options, redirect_count, nav_token) {
481481
}
482482
}
483483
});
484+
484485
if (options.invalidateAll) {
485486
// TODO the ticks shouldn't be necessary, something inside Svelte itself is buggy
486487
// when a query in a layout that still exists after page change is refreshed earlier than this
@@ -496,7 +497,6 @@ export async function _goto(url, options, redirect_count, nav_token) {
496497
});
497498
});
498499
}
499-
return result;
500500
}
501501

502502
/** @param {import('./types.js').NavigationIntent} intent */
@@ -1283,6 +1283,7 @@ async function load_root_error_page({ status, error, url, route }) {
12831283
});
12841284
} catch (error) {
12851285
if (error instanceof Redirect) {
1286+
// @ts-expect-error TODO investigate this
12861287
return _goto(new URL(error.location, location.href), {}, 0);
12871288
}
12881289

@@ -1577,7 +1578,7 @@ async function navigate({
15771578
if (navigation_result.type === 'redirect') {
15781579
// whatwg fetch spec https://fetch.spec.whatwg.org/#http-redirect-fetch says to error after 20 redirects
15791580
if (redirect_count < 20) {
1580-
return navigate({
1581+
await navigate({
15811582
type,
15821583
url: new URL(navigation_result.location, url),
15831584
popped,
@@ -1588,6 +1589,9 @@ async function navigate({
15881589
redirect_count: redirect_count + 1,
15891590
nav_token
15901591
});
1592+
1593+
nav.fulfil(undefined);
1594+
return;
15911595
}
15921596

15931597
navigation_result = await load_root_error_page({
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<script>
2+
import { beforeNavigate } from '$app/navigation';
3+
4+
beforeNavigate(({ complete }) => {
5+
complete.then(() => {
6+
console.log('complete');
7+
});
8+
});
9+
</script>
10+
11+
<a href="/navigation-lifecycle/before-navigate/redirect">redirect</a>

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,12 @@ test.describe('Navigation lifecycle functions', () => {
240240
await expect(page.locator('pre')).toHaveText('1 false link');
241241
});
242242

243+
test("beforeNavigate's complete fulfills after redirect", async ({ page, clicknav }) => {
244+
await page.goto('/navigation-lifecycle/before-navigate/complete');
245+
clicknav('a[href="/navigation-lifecycle/before-navigate/redirect"]');
246+
expect(await page.waitForEvent('console', (msg) => msg.text() === 'complete')).toBeTruthy();
247+
});
248+
243249
test('afterNavigate calls callback', async ({ page, clicknav }) => {
244250
await page.goto('/navigation-lifecycle/after-navigate/a');
245251
expect(await page.textContent('h1')).toBe(

0 commit comments

Comments
 (0)