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

Update to merge props from GSSP methods with _app pageProps #11709

Merged
merged 7 commits into from
Apr 7, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
11 changes: 11 additions & 0 deletions errors/gssp-pageprops-conflict.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# getStaticProps/getServerSideProps `pageProps` Conflict

#### Why This Error Occurred

In your `_app` you returned `pageProps` for a page that uses `getStaticProps` or `getServerSideProps`.

The `pageProps` value will be overridden with the result from `getStaticProps` or `getServerSideProps`

#### Possible Ways to Fix It

Only return `pageProps` from your custom `_app` when needed for a page with `getInitialProps`
12 changes: 12 additions & 0 deletions packages/next/next-server/server/render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,18 @@ export async function renderToHTML(
props[STATIC_PROPS_ID] = true
}

if (
(isSSG || getServerSideProps) &&
props.pageProps &&
process.env.NODE_ENV !== 'production'
) {
console.warn(
`"pageProps" was returned from "_app" for page "${pathname}" with ${
isSSG ? 'getStaticProps' : 'getServerSideProps'
} and will be overridden.\nSee here for more info: https://err.sh/next.js/gssp-pageprops-conflict`
)
}

let previewData: string | false | object | undefined

if ((isSSG || getServerSideProps) && !isFallback) {
Expand Down
9 changes: 9 additions & 0 deletions test/integration/gssp-pageProps-conflict/pages/_app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const App = ({ Component, pageProps }) => <Component {...pageProps} />

App.getInitialProps = () => ({
pageProps: {
hello: 'world',
},
})

export default App
63 changes: 63 additions & 0 deletions test/integration/gssp-pageProps-conflict/test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* eslint-env jest */
/* global jasmine */
import fs from 'fs-extra'
import { join } from 'path'
import { findPort, launchApp, killApp, renderViaHTTP } from 'next-test-utils'

jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 2
const appDir = join(__dirname, '..')
const indexPage = join(appDir, 'pages/index.js')

let stderr = ''
let appPort
let app

describe('pageProps GSSP conflict', () => {
it('should show warning for pageProps and getServerSideProps conflict', async () => {
appPort = await findPort()
app = await launchApp(appDir, appPort, {
onStderr(msg) {
stderr += msg || ''
},
})
await fs.writeFile(
indexPage,
`
export const getServerSideProps = () => ({ props: { hi: 'hi' } })
export default () => 'hello from gssp'
`
)
const html = await renderViaHTTP(appPort, '/')
await fs.remove(indexPage)
await killApp(app)

expect(html).toContain('hello from gssp')
expect(stderr).toContain(
`"pageProps" was returned from "_app" for page "/" with getServerSideProps`
)
})

it('should show warning for pageProps and getStaticProps conflict', async () => {
appPort = await findPort()
app = await launchApp(appDir, appPort, {
onStderr(msg) {
stderr += msg || ''
},
})
await fs.writeFile(
indexPage,
`
export const getStaticProps = () => ({ props: { hi: 'hi' } })
export default () => 'hello from gsp'
`
)
const html = await renderViaHTTP(appPort, '/')
await fs.remove(indexPage)
await killApp(app)

expect(html).toContain('hello from gsp')
expect(stderr).toContain(
`"pageProps" was returned from "_app" for page "/" with getStaticProps`
)
})
})