Skip to content

Commit 5127d86

Browse files
authored
refactor(router-core): avoid creating promises when not necessary (#7582)
1 parent 689d88e commit 5127d86

2 files changed

Lines changed: 8 additions & 7 deletions

File tree

packages/history/src/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ export function createBrowserHistory(opts?: {
340340

341341
// We need to track the current scheduled update to prevent
342342
// multiple updates from being scheduled at the same time.
343-
let scheduled: Promise<void> | undefined
343+
let scheduled: undefined | boolean
344344

345345
// This function flushes the next update to the browser history
346346
const flush = () => {
@@ -363,7 +363,7 @@ export function createBrowserHistory(opts?: {
363363

364364
// Reset the nextIsPush flag and clear the scheduled update
365365
next = undefined
366-
scheduled = undefined
366+
scheduled = false
367367
rollbackLocation = undefined
368368
}
369369

@@ -391,7 +391,8 @@ export function createBrowserHistory(opts?: {
391391

392392
if (!scheduled) {
393393
// Schedule an update to the browser history
394-
scheduled = Promise.resolve().then(() => flush())
394+
scheduled = true
395+
queueMicrotask(() => flush())
395396
}
396397
}
397398

packages/router-core/src/router.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2294,7 +2294,7 @@ export class RouterCore<
22942294

22952295
// Clear pending location after commit starts
22962296
// We do this on next microtask to allow synchronous navigate calls to chain
2297-
Promise.resolve().then(() => {
2297+
queueMicrotask(() => {
22982298
if (this.pendingBuiltLocation === location) {
22992299
this.pendingBuiltLocation = undefined
23002300
}
@@ -2354,7 +2354,7 @@ export class RouterCore<
23542354
`Blocked navigation to dangerous protocol: ${reloadHref}`,
23552355
)
23562356
}
2357-
return Promise.resolve()
2357+
return
23582358
}
23592359

23602360
// Check blockers for external URLs unless ignoreBlocker is true
@@ -2370,7 +2370,7 @@ export class RouterCore<
23702370
action: 'PUSH',
23712371
})
23722372
if (shouldBlock) {
2373-
return Promise.resolve()
2373+
return
23742374
}
23752375
}
23762376
}
@@ -2381,7 +2381,7 @@ export class RouterCore<
23812381
} else {
23822382
window.location.href = reloadHref
23832383
}
2384-
return Promise.resolve()
2384+
return
23852385
}
23862386

23872387
return this.buildAndCommitLocation({

0 commit comments

Comments
 (0)