Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call getInitialProps on Component when it’s not defined on App #9287

Merged
merged 2 commits into from
Nov 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions packages/next/next-server/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,31 +240,37 @@ export async function loadGetInitialProps<
C extends BaseContext,
IP = {},
P = {}
>(Component: NextComponentType<C, IP, P>, ctx: C): Promise<IP> {
>(App: NextComponentType<C, IP, P>, ctx: C): Promise<IP> {
if (process.env.NODE_ENV !== 'production') {
if (Component.prototype && Component.prototype.getInitialProps) {
if (App.prototype && App.prototype.getInitialProps) {
const message = `"${getDisplayName(
Component
App
)}.getInitialProps()" is defined as an instance method - visit https://err.sh/zeit/next.js/get-initial-props-as-an-instance-method for more information.`
throw new Error(message)
}
}
// when called from _app `ctx` is nested in `ctx`
const res = ctx.res || (ctx.ctx && ctx.ctx.res)

if (!Component.getInitialProps) {
if (!App.getInitialProps) {
if (ctx.ctx && ctx.Component) {
// @ts-ignore pageProps default
return {
pageProps: await loadGetInitialProps(ctx.Component, ctx.ctx),
}
}
return {} as any
}

const props = await Component.getInitialProps(ctx)
const props = await App.getInitialProps(ctx)

if (res && isResSent(res)) {
return props
}

if (!props) {
const message = `"${getDisplayName(
Component
App
)}.getInitialProps()" should resolve to an object. But found "${props}" instead.`
throw new Error(message)
}
Expand All @@ -273,7 +279,7 @@ export async function loadGetInitialProps<
if (Object.keys(props).length === 0 && !ctx.ctx) {
console.warn(
`${getDisplayName(
Component
App
)} returned an empty object from \`getInitialProps\`. This de-optimizes and prevents automatic static optimization. https://err.sh/zeit/next.js/empty-object-getInitialProps`
)
}
Expand Down
3 changes: 3 additions & 0 deletions test/integration/app-functional/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
crossOrigin: 'anonymous'
}
17 changes: 17 additions & 0 deletions test/integration/app-functional/pages/_app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function MyApp ({ Component, pageProps }) {
return <Component {...pageProps} />
}

// Only uncomment this method if you have blocking data requirements for
// every single page in your application. This disables the ability to
// perform automatic static optimization, causing every page in your app to
// be server-side rendered.
//
// MyApp.getInitialProps = async (appContext) => {
// // calls page's `getInitialProps` and fills `appProps.pageProps`
// const appProps = await App.getInitialProps(appContext);
//
// return { ...appProps }
// }

export default MyApp
11 changes: 11 additions & 0 deletions test/integration/app-functional/pages/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function Page ({ message }) {
return <div>{message}</div>
}

Page.getInitialProps = async ({ req }) => {
return {
message: 'Hello World!!!'
}
}

export default Page
9 changes: 9 additions & 0 deletions test/integration/app-functional/shared-module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
let moduleState = 'INITIAL'

export function setState (state) {
moduleState = state
}

export default function currentState () {
return moduleState
}
32 changes: 32 additions & 0 deletions test/integration/app-functional/test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* eslint-env jest */
/* global jasmine */
import { join } from 'path'
import { renderViaHTTP, findPort, launchApp, killApp } from 'next-test-utils'

const context = {
output: ''
}
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 5

const collectOutput = message => {
context.output += message
}

describe('Document and App', () => {
beforeAll(async () => {
context.appPort = await findPort()
context.server = await launchApp(join(__dirname, '../'), context.appPort, {
onStdout: collectOutput,
onStderr: collectOutput
})

// pre-build all pages at the start
await Promise.all([renderViaHTTP(context.appPort, '/')])
})
afterAll(() => killApp(context.server))

it('should not have any missing key warnings', async () => {
const html = await renderViaHTTP(context.appPort, '/')
expect(html).toMatch(/<div>Hello World!!!<\/div>/)
})
})