Skip to content

Commit 3a52dbc

Browse files
Sheraffautofix-ci[bot]
authored andcommitted
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 2846e6a commit 3a52dbc

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
@@ -2923,33 +2923,22 @@ export class RouterCore<
29232923
}
29242924

29252925
try {
2926-
await new Promise<void>((resolveAll, rejectAll) => {
2927-
;(async () => {
2928-
try {
2929-
// Execute all beforeLoads one by one
2930-
for (let i = 0; i < innerLoadContext.matches.length; i++) {
2931-
const beforeLoad = this.handleBeforeLoad(innerLoadContext, i)
2932-
if (isPromise(beforeLoad)) await beforeLoad
2933-
}
2934-
2935-
// Execute all loaders in parallel
2936-
const max =
2937-
innerLoadContext.firstBadMatchIndex ??
2938-
innerLoadContext.matches.length
2939-
for (let i = 0; i < max; i++) {
2940-
innerLoadContext.matchPromises.push(
2941-
this.loadRouteMatch(innerLoadContext, i),
2942-
)
2943-
}
2926+
// Execute all beforeLoads one by one
2927+
for (let i = 0; i < innerLoadContext.matches.length; i++) {
2928+
const beforeLoad = this.handleBeforeLoad(innerLoadContext, i)
2929+
if (isPromise(beforeLoad)) await beforeLoad
2930+
}
29442931

2945-
await Promise.all(innerLoadContext.matchPromises)
2932+
// Execute all loaders in parallel
2933+
const max =
2934+
innerLoadContext.firstBadMatchIndex ?? innerLoadContext.matches.length
2935+
for (let i = 0; i < max; i++) {
2936+
innerLoadContext.matchPromises.push(
2937+
this.loadRouteMatch(innerLoadContext, i),
2938+
)
2939+
}
2940+
await Promise.all(innerLoadContext.matchPromises)
29462941

2947-
resolveAll()
2948-
} catch (err) {
2949-
rejectAll(err)
2950-
}
2951-
})()
2952-
})
29532942
const readyPromise = this.triggerOnReady(innerLoadContext)
29542943
if (isPromise(readyPromise)) await readyPromise
29552944
} catch (err) {

0 commit comments

Comments
 (0)