diff --git a/packages/next/next-server/server/render.tsx b/packages/next/next-server/server/render.tsx index 31bdb9ff244c4..40257d88b605f 100644 --- a/packages/next/next-server/server/render.tsx +++ b/packages/next/next-server/server/render.tsx @@ -584,7 +584,7 @@ export async function renderToHTML( data.unstable_revalidate = false } - props.pageProps = data.props + props.pageProps = Object.assign({}, props.pageProps, data.props) // pass up revalidate and props for export // TODO: change this to a different passing mechanism ;(renderOpts as any).revalidate = data.unstable_revalidate @@ -633,7 +633,7 @@ export async function renderToHTML( ) } - props.pageProps = data.props + props.pageProps = Object.assign({}, props.pageProps, data.props) ;(renderOpts as any).pageData = props } } catch (err) { diff --git a/test/integration/gssp-pageProps-merge/pages/_app.js b/test/integration/gssp-pageProps-merge/pages/_app.js new file mode 100644 index 0000000000000..46d764246d470 --- /dev/null +++ b/test/integration/gssp-pageProps-merge/pages/_app.js @@ -0,0 +1,10 @@ +const App = ({ Component, pageProps }) => + +App.getInitialProps = () => ({ + pageProps: { + hi: 'override me', + hello: 'world', + }, +}) + +export default App diff --git a/test/integration/gssp-pageProps-merge/pages/gsp.js b/test/integration/gssp-pageProps-merge/pages/gsp.js new file mode 100644 index 0000000000000..1bcf3bb50cc08 --- /dev/null +++ b/test/integration/gssp-pageProps-merge/pages/gsp.js @@ -0,0 +1,2 @@ +export const getStaticProps = () => ({ props: { hi: 'hi' } }) +export default props =>

{JSON.stringify(props)}

diff --git a/test/integration/gssp-pageProps-merge/pages/gssp.js b/test/integration/gssp-pageProps-merge/pages/gssp.js new file mode 100644 index 0000000000000..629b687ee2373 --- /dev/null +++ b/test/integration/gssp-pageProps-merge/pages/gssp.js @@ -0,0 +1,2 @@ +export const getServerSideProps = () => ({ props: { hi: 'hi' } }) +export default props =>

{JSON.stringify(props)}

diff --git a/test/integration/gssp-pageProps-merge/test/index.test.js b/test/integration/gssp-pageProps-merge/test/index.test.js new file mode 100644 index 0000000000000..127482e1af34f --- /dev/null +++ b/test/integration/gssp-pageProps-merge/test/index.test.js @@ -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() + }) +})