Skip to content

Commit 891b55c

Browse files
committed
lower still
1 parent 9d4b7c2 commit 891b55c

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ describe("Store doesn't update *too many* times during navigation", () => {
133133

134134
test('sync beforeLoad', async () => {
135135
const params = setup({
136-
beforeLoad: () => {},
136+
beforeLoad: () => ({ foo: 'bar' }),
137137
loader: () => new Promise<void>((resolve) => setTimeout(resolve, 100)),
138138
defaultPendingMs: 100,
139139
defaultPendingMinMs: 300,
@@ -144,6 +144,6 @@ describe("Store doesn't update *too many* times during navigation", () => {
144144
// This number should be as small as possible to minimize the amount of work
145145
// that needs to be done during a navigation.
146146
// Any change that increases this number should be investigated.
147-
expect(updates).toBe(14)
147+
expect(updates).toBe(13)
148148
})
149149
})

packages/router-core/src/router.ts

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2371,24 +2371,27 @@ export class RouterCore<
23712371
): void | Promise<void> => {
23722372
const match = this.getMatch(matchId)!
23732373
const abortController = new AbortController()
2374+
const parentMatchId = innerLoadContext.matches[index - 1]?.id
2375+
const parentMatch = parentMatchId
2376+
? this.getMatch(parentMatchId)!
2377+
: undefined
2378+
const parentMatchContext =
2379+
parentMatch?.context ?? this.options.context ?? undefined
2380+
const context = {
2381+
...parentMatchContext,
2382+
...match.__routeContext,
2383+
}
23742384

2375-
const pending = () => {
2376-
const parentMatchId = innerLoadContext.matches[index - 1]?.id
2377-
const parentMatch = parentMatchId
2378-
? this.getMatch(parentMatchId)!
2379-
: undefined
2380-
const parentMatchContext =
2381-
parentMatch?.context ?? this.options.context ?? undefined
2385+
let isPending = false
23822386

2387+
const pending = () => {
2388+
isPending = true
23832389
innerLoadContext.updateMatch(matchId, (prev) => ({
23842390
...prev,
23852391
isFetching: 'beforeLoad',
23862392
fetchCount: prev.fetchCount + 1,
23872393
abortController,
2388-
context: {
2389-
...parentMatchContext,
2390-
...prev.__routeContext,
2391-
},
2394+
context,
23922395
}))
23932396
}
23942397

@@ -2439,11 +2442,12 @@ export class RouterCore<
24392442
return
24402443
}
24412444

2442-
pending()
2443-
24442445
const updateContext = (beforeLoadContext: any) => {
24452446
if (beforeLoadContext === undefined) {
2446-
resolve()
2447+
batch(() => {
2448+
if (!isPending) pending()
2449+
resolve()
2450+
})
24472451
return
24482452
}
24492453
if (isRedirect(beforeLoadContext) || isNotFound(beforeLoadContext)) {
@@ -2455,6 +2459,7 @@ export class RouterCore<
24552459
)
24562460
}
24572461
batch(() => {
2462+
if (!isPending) pending()
24582463
innerLoadContext.updateMatch(matchId, (prev) => ({
24592464
...prev,
24602465
__beforeLoadContext: beforeLoadContext,
@@ -2466,7 +2471,7 @@ export class RouterCore<
24662471
resolve()
24672472
})
24682473
}
2469-
const { search, params, context, cause } = this.getMatch(matchId)!
2474+
const { search, params, cause } = match
24702475
const preload = this.resolvePreload(innerLoadContext, matchId)
24712476
const beforeLoadFnContext: BeforeLoadContextOptions<
24722477
any,
@@ -2493,6 +2498,7 @@ export class RouterCore<
24932498
try {
24942499
const beforeLoadContext = route.options.beforeLoad(beforeLoadFnContext)
24952500
if (isPromise(beforeLoadContext)) {
2501+
pending()
24962502
return beforeLoadContext.then(updateContext).catch((err) => {
24972503
this.handleSerialError(innerLoadContext, index, err, 'BEFORE_LOAD')
24982504
})

0 commit comments

Comments
 (0)