Unverified Commit 2d0ad7d4 authored by Joe Haddad's avatar Joe Haddad Committed by GitHub

Refactor Router#getInitialProps (#9853)

This PR refactors `Router#getInitialProps` in preparation for dedicated SSG fetching logic. This PR should have no functional change -- please review it carefully.
parent 356e5caf
...@@ -624,62 +624,65 @@ export default class Router implements BaseRouter { ...@@ -624,62 +624,65 @@ export default class Router implements BaseRouter {
return Component return Component
} }
async getInitialProps( _getData<T>(fn: () => Promise<T>): Promise<T> {
Component: ComponentType,
ctx: NextPageContext
): Promise<any> {
let cancelled = false let cancelled = false
const cancel = () => { const cancel = () => {
cancelled = true cancelled = true
} }
this.clc = cancel this.clc = cancel
const { Component: App } = this.components['/_app'] return fn().then(data => {
let props if (cancel === this.clc) {
this.clc = null
}
if ((Component as any).__NEXT_SPR) { if (cancelled) {
let status: any const err: any = new Error('Loading initial props cancelled')
// pathname should have leading slash err.cancelled = true
let { pathname } = parse(ctx.asPath || ctx.pathname) throw err
pathname = !pathname || pathname === '/' ? '/index' : pathname }
props = await fetch( return data
// @ts-ignore __NEXT_DATA__ })
`/_next/data/${__NEXT_DATA__.buildId}${pathname}.json` }
)
.then(res => { getInitialProps(
if (!res.ok) { Component: ComponentType,
status = res.status ctx: NextPageContext
throw new Error('failed to load prerender data') ): Promise<any> {
} const { Component: App } = this.components['/_app']
return res.json() const AppTree = this._wrapApp(App)
}) ctx.AppTree = AppTree
.catch((err: Error) => { return this._getData(() => {
console.error(`Failed to load data`, status, err) if ((Component as any).__NEXT_SPR) {
window.location.href = pathname! let status: any
return new Promise(() => {}) // pathname should have leading slash
}) let { pathname } = parse(ctx.asPath || ctx.pathname)
} else { pathname = !pathname || pathname === '/' ? '/index' : pathname
const AppTree = this._wrapApp(App)
ctx.AppTree = AppTree return fetch(
props = await loadGetInitialProps<AppContextType<Router>>(App, { // @ts-ignore __NEXT_DATA__
`/_next/data/${__NEXT_DATA__.buildId}${pathname}.json`
)
.then(res => {
if (!res.ok) {
status = res.status
throw new Error('failed to load prerender data')
}
return res.json()
})
.catch((err: Error) => {
console.error(`Failed to load data`, status, err)
window.location.href = pathname!
return new Promise(() => {})
})
}
return loadGetInitialProps<AppContextType<Router>>(App, {
AppTree, AppTree,
Component, Component,
router: this, router: this,
ctx, ctx,
}) })
} })
if (cancel === this.clc) {
this.clc = null
}
if (cancelled) {
const err: any = new Error('Loading initial props cancelled')
err.cancelled = true
throw err
}
return props
} }
abortComponentLoad(as: string): void { abortComponentLoad(as: string): void {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment