1
0
Fork 0
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:
Arunoda Susiripala 2017-06-23 10:46:55 +05:30 committed by GitHub
parent 213d7a82d1
commit 8c6550c504

View file

@ -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