Skip to content

Commit 78ef1c9

Browse files
refactor(router-core): loadMatches extra microtask (#4967)
The `loadMatches` has an unnecessary complicated async setup, probably due to how complicated the function was before recent cleanups: ```ts async function loadMatches() { try { await new Promise((resolve, reject) => { ;(async () => { try { // the logic resolve() } catch (err) { reject(err) } })() }) // after promise } catch (err) { // error handling } } ``` Aside from some scheduling differences due to unnecessary promises in the above example, this can be simplified down to this: ```ts async function loadMatches() { try { // the logic // after promise } catch (err) { // error handling } } ``` This is what this PR does. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - New Features - No user-facing changes. - Refactor - Simplified internal async flow for route loading, removing redundant promise wrapping. - Maintains existing behavior and public API; no action required from users. - More consistent error handling and slightly reduced overhead during parallel loads. - Improves maintainability and prepares the codebase for future enhancements. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent f6fcb1b commit 78ef1c9

File tree

2 files changed

+15
-26
lines changed

2 files changed

+15
-26
lines changed

packages/react-router/tests/store-updates-during-navigation.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ describe("Store doesn't update *too many* times during navigation", () => {
183183
// This number should be as small as possible to minimize the amount of work
184184
// that needs to be done during a navigation.
185185
// Any change that increases this number should be investigated.
186-
expect(updates).toBe(8)
186+
expect(updates).toBe(7)
187187
})
188188

189189
test('hover preload, then navigate, w/ async loaders', async () => {

packages/router-core/src/router.ts

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2930,33 +2930,22 @@ export class RouterCore<
29302930
}
29312931

29322932
try {
2933-
await new Promise<void>((resolveAll, rejectAll) => {
2934-
;(async () => {
2935-
try {
2936-
// Execute all beforeLoads one by one
2937-
for (let i = 0; i < innerLoadContext.matches.length; i++) {
2938-
const beforeLoad = this.handleBeforeLoad(innerLoadContext, i)
2939-
if (isPromise(beforeLoad)) await beforeLoad
2940-
}
2941-
2942-
// Execute all loaders in parallel
2943-
const max =
2944-
innerLoadContext.firstBadMatchIndex ??
2945-
innerLoadContext.matches.length
2946-
for (let i = 0; i < max; i++) {
2947-
innerLoadContext.matchPromises.push(
2948-
this.loadRouteMatch(innerLoadContext, i),
2949-
)
2950-
}
2933+
// Execute all beforeLoads one by one
2934+
for (let i = 0; i < innerLoadContext.matches.length; i++) {
2935+
const beforeLoad = this.handleBeforeLoad(innerLoadContext, i)
2936+
if (isPromise(beforeLoad)) await beforeLoad
2937+
}
29512938

2952-
await Promise.all(innerLoadContext.matchPromises)
2939+
// Execute all loaders in parallel
2940+
const max =
2941+
innerLoadContext.firstBadMatchIndex ?? innerLoadContext.matches.length
2942+
for (let i = 0; i < max; i++) {
2943+
innerLoadContext.matchPromises.push(
2944+
this.loadRouteMatch(innerLoadContext, i),
2945+
)
2946+
}
2947+
await Promise.all(innerLoadContext.matchPromises)
29532948

2954-
resolveAll()
2955-
} catch (err) {
2956-
rejectAll(err)
2957-
}
2958-
})()
2959-
})
29602949
const readyPromise = this.triggerOnReady(innerLoadContext)
29612950
if (isPromise(readyPromise)) await readyPromise
29622951
} catch (err) {

0 commit comments

Comments
 (0)