Skip to content

Commit

Permalink
Improve page loader types (vercel#16145)
Browse files Browse the repository at this point in the history
  • Loading branch information
Timer authored and Markus Lautenbach committed Aug 20, 2020
1 parent 84b00a2 commit 01de1fb
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions packages/next/client/page-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ function appendLink(
})
}

type PageCacheEntry = { error?: any; page?: any; mod?: any }
export type GoodPageCache = { page: any; mod: any }
export type PageCacheEntry = { error: any } | GoodPageCache

export default class PageLoader {
private buildId: string
Expand Down Expand Up @@ -225,26 +226,29 @@ export default class PageLoader {
)
}

loadPage(route: string) {
loadPage(route: string): Promise<GoodPageCache> {
route = normalizeRoute(route)

return new Promise((resolve, reject) => {
return new Promise<GoodPageCache>((resolve, reject) => {
// If there's a cached version of the page, let's use it.
const cachedPage = this.pageCache[route]
if (cachedPage) {
const { error, page, mod } = cachedPage
error ? reject(error) : resolve({ page, mod })
if ('error' in cachedPage) {
reject(cachedPage.error)
} else {
resolve(cachedPage)
}
return
}

const fire = ({ error, page, mod }: PageCacheEntry) => {
const fire = (pageToCache: PageCacheEntry) => {
this.pageRegisterEvents.off(route, fire)
delete this.loadingRoutes[route]

if (error) {
reject(error)
if ('error' in pageToCache) {
reject(pageToCache.error)
} else {
resolve({ page, mod })
resolve(pageToCache)
}
}

Expand Down

0 comments on commit 01de1fb

Please sign in to comment.