Skip to content

Commit

Permalink
feat(loaders): thrown navigation results take precedence
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Jan 6, 2024
1 parent 3f01155 commit 2aaaf56
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
22 changes: 22 additions & 0 deletions src/data-fetching_new/navigation-guard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@ describe('navigation-guard', () => {
await vi.runOnlyPendingTimersAsync()
l1.resolve(new NavigationResult('/#ok'))
await router.getPendingNavigation()
expect(selectNavigationResult).toHaveBeenCalledTimes(1)
expect(router.currentRoute.value.fullPath).toBe('/#ok')
})

Expand Down Expand Up @@ -429,5 +430,26 @@ describe('navigation-guard', () => {
await router.getPendingNavigation()
expect(router.currentRoute.value.fullPath).toBe('/fetch')
})

it('immediately stops if a NavigationResult is thrown instead of returned inside the loader', async () => {
const router = getRouter()
const l1 = mockedLoader()
router.addRoute({
name: '_test',
path: '/fetch',
component,
meta: {
loaders: [l1.loader],
},
})

router.push('/fetch')
await vi.runOnlyPendingTimersAsync()
const r1 = new NavigationResult('/#ok')
l1.reject(r1)
await router.getPendingNavigation().catch(() => {})
expect(selectNavigationResult).not.toHaveBeenCalled()
expect(router.currentRoute.value.fullPath).toBe('/#ok')
})
})
})
9 changes: 8 additions & 1 deletion src/data-fetching_new/navigation-guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,14 @@ export function setupLoaderGuard({
return selectNavigationResult(to.meta[NAVIGATION_RESULTS_KEY]!)
}
})
// no catch so errors are propagated to the router
.catch((error) =>
error instanceof NavigationResult
? error.value
: // let the error propagate to router.onError()
// we use never because the rejection means we never resolve a value and using anything else
// will not be valid from the navigation guard's perspective
Promise.reject<never>(error)
)
})

// listen to duplicated navigation failures to reset the pendingTo and pendingLoad
Expand Down

0 comments on commit 2aaaf56

Please sign in to comment.