mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
Fix a potential race condition in router when doing prefetching. (#2317)
This commit is contained in:
parent
213d7a82d1
commit
8c6550c504
|
@ -30,17 +30,10 @@ export default class PageLoader {
|
||||||
loadPage (route) {
|
loadPage (route) {
|
||||||
route = this.normalizeRoute(route)
|
route = this.normalizeRoute(route)
|
||||||
|
|
||||||
const cachedPage = this.pageCache[route]
|
|
||||||
if (cachedPage) {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
if (cachedPage.error) return reject(cachedPage.error)
|
|
||||||
return resolve(cachedPage.page)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const fire = ({ error, page }) => {
|
const fire = ({ error, page }) => {
|
||||||
this.pageRegisterEvents.off(route, fire)
|
this.pageRegisterEvents.off(route, fire)
|
||||||
|
delete this.loadingRoutes[route]
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
reject(error)
|
reject(error)
|
||||||
|
@ -49,6 +42,15 @@ export default class PageLoader {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If there's a cached version of the page, let's use it.
|
||||||
|
const cachedPage = this.pageCache[route]
|
||||||
|
if (cachedPage) {
|
||||||
|
const { error, page } = cachedPage
|
||||||
|
error ? reject(error) : resolve(page)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Register a listener to get the page
|
||||||
this.pageRegisterEvents.on(route, fire)
|
this.pageRegisterEvents.on(route, fire)
|
||||||
|
|
||||||
// If the page is loading via SSR, we need to wait for it
|
// If the page is loading via SSR, we need to wait for it
|
||||||
|
|
Loading…
Reference in a new issue