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 5 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
6 changes: 4 additions & 2 deletions packages/next/next-server/server/render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,8 @@ export async function renderToHTML(
data.unstable_revalidate = false
}

props.pageProps = data.props
if (!props.pageProps) props.pageProps = {}
Object.assign(props.pageProps, data.props)
ijjk marked this conversation as resolved.
Show resolved Hide resolved
// pass up revalidate and props for export
// TODO: change this to a different passing mechanism
;(renderOpts as any).revalidate = data.unstable_revalidate
Expand Down Expand Up @@ -633,7 +634,8 @@ export async function renderToHTML(
)
}

props.pageProps = data.props
if (!props.pageProps) props.pageProps = {}
Object.assign(props.pageProps, data.props)
ijjk marked this conversation as resolved.
Show resolved Hide resolved
;(renderOpts as any).pageData = props
}
} catch (err) {
Expand Down
10 changes: 10 additions & 0 deletions test/integration/gssp-pageProps-merge/pages/_app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const App = ({ Component, pageProps }) => <Component {...pageProps} />

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

export default App
2 changes: 2 additions & 0 deletions test/integration/gssp-pageProps-merge/pages/gsp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const getStaticProps = () => ({ props: { hi: 'hi' } })
export default props => <p>{JSON.stringify(props)}</p>
2 changes: 2 additions & 0 deletions test/integration/gssp-pageProps-merge/pages/gssp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const getServerSideProps = () => ({ props: { hi: 'hi' } })
export default props => <p>{JSON.stringify(props)}</p>
81 changes: 81 additions & 0 deletions test/integration/gssp-pageProps-merge/test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/* eslint-env jest */
/* global jasmine */
import fs from 'fs-extra'
import { join } from 'path'
import cheerio from 'cheerio'
import {
findPort,
launchApp,
killApp,
renderViaHTTP,
nextBuild,
nextStart,
} from 'next-test-utils'

jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 2
const appDir = join(__dirname, '..')
const nextConfig = join(appDir, 'next.config.js')

let appPort
let app

const runTests = () => {
it('should merge _app pageProps and getServerSideProps props', async () => {
const html = await renderViaHTTP(appPort, '/gssp')
const $ = cheerio.load(html)
expect(JSON.parse($('p').text())).toEqual({ hi: 'hi', hello: 'world' })
})

it('should merge _app pageProps and getStaticProps props', async () => {
const html = await renderViaHTTP(appPort, '/gsp')
const $ = cheerio.load(html)
expect(JSON.parse($('p').text())).toEqual({ hi: 'hi', hello: 'world' })
})
}

describe('pageProps GSSP conflict', () => {
describe('dev mode', () => {
beforeAll(async () => {
appPort = await findPort()
app = await launchApp(appDir, appPort)
})
afterAll(() => killApp(app))

runTests()
})

describe('production mode', () => {
beforeAll(async () => {
const { code } = await nextBuild(appDir)
if (code !== 0) throw new Error(`build failed with code ${code}`)

appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(() => killApp(app))

runTests()
})

describe('serverless mode', () => {
beforeAll(async () => {
await fs.writeFile(
nextConfig,
`module.exports = {
target: 'experimental-serverless-trace'
}`
)
const { code } = await nextBuild(appDir)
if (code !== 0) throw new Error(`build failed with code ${code}`)

appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(async () => {
await fs.remove(nextConfig)
await killApp(app)
})

runTests()
})
})