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 {
return Component
}
async getInitialProps(
Component: ComponentType,
ctx: NextPageContext
): Promise<any> {
_getData<T>(fn: () => Promise<T>): Promise<T> {
let cancelled = false
const cancel = () => {
cancelled = true
}
this.clc = cancel
const { Component: App } = this.components['/_app']
let props
return fn().then(data => {
if (cancel === this.clc) {
this.clc = null
}
if ((Component as any).__NEXT_SPR) {
let status: any
// pathname should have leading slash
let { pathname } = parse(ctx.asPath || ctx.pathname)
pathname = !pathname || pathname === '/' ? '/index' : pathname
if (cancelled) {
const err: any = new Error('Loading initial props cancelled')
err.cancelled = true
throw err
}
props = await fetch(
// @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(() => {})
})
} else {
const AppTree = this._wrapApp(App)
ctx.AppTree = AppTree
props = await loadGetInitialProps<AppContextType<Router>>(App, {
return data
})
}
getInitialProps(
Component: ComponentType,
ctx: NextPageContext
): Promise<any> {
const { Component: App } = this.components['/_app']
const AppTree = this._wrapApp(App)
ctx.AppTree = AppTree
return this._getData(() => {
if ((Component as any).__NEXT_SPR) {
let status: any
// pathname should have leading slash
let { pathname } = parse(ctx.asPath || ctx.pathname)
pathname = !pathname || pathname === '/' ? '/index' : pathname
return fetch(
// @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,
Component,
router: this,
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 {
......
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