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

Add gip identifiers to NEXT_DATA #11835

Merged
merged 2 commits into from
Apr 13, 2020
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
2 changes: 2 additions & 0 deletions packages/next/next-server/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ export type NEXT_DATA = {
gsp?: boolean
gssp?: boolean
customServer?: boolean
gip?: boolean
appGip?: boolean
}

/**
Expand Down
8 changes: 8 additions & 0 deletions packages/next/next-server/server/render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ function renderDocument(
gsp,
gssp,
customServer,
gip,
appGip,
}: RenderOpts & {
props: any
docProps: DocumentInitialProps
Expand All @@ -218,6 +220,8 @@ function renderDocument(
gsp?: boolean
gssp?: boolean
customServer?: boolean
gip?: boolean
appGip?: boolean
}
): string {
return (
Expand All @@ -241,6 +245,8 @@ function renderDocument(
gsp, // whether the page is getStaticProps
gssp, // whether the page is getServerSideProps
customServer, // whether the user is using a custom server
gip, // whether the page has getInitialProps
appGip, // whether the _app has getInitialProps
},
dangerousAsPath,
canonicalBase,
Expand Down Expand Up @@ -777,6 +783,8 @@ export async function renderToHTML(
polyfillFiles,
gsp: !!getStaticProps ? true : undefined,
gssp: !!getServerSideProps ? true : undefined,
gip: hasPageGetInitialProps ? true : undefined,
appGip: !defaultAppGetInitialProps ? true : undefined,
})

if (inAmpMode && html) {
Expand Down
1 change: 1 addition & 0 deletions test/integration/gip-identifier/pages/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default () => 'hi'
98 changes: 98 additions & 0 deletions test/integration/gip-identifier/test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/* eslint-env jest */
/* global jasmine */
import cheerio from 'cheerio'
import fs from 'fs-extra'
import {
findPort,
killApp,
launchApp,
nextBuild,
nextStart,
renderViaHTTP,
} from 'next-test-utils'
import { join } from 'path'

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

let app
let appPort
let indexPageContent

const runTests = isDev => {
const getData = async () => {
if (isDev) {
appPort = await findPort()
app = await launchApp(appDir, appPort)
} else {
const { code } = await nextBuild(appDir)
if (code !== 0) throw new Error(`build faild, exit code: ${code}`)
appPort = await findPort()
app = await nextStart(appDir, appPort)
}
const html = await renderViaHTTP(appPort, '/')
await killApp(app)
const $ = cheerio.load(html)
return JSON.parse($('#__NEXT_DATA__').text())
}

it('should not have gip or appGip in NEXT_DATA for page without getInitialProps', async () => {
const data = await getData()
expect(data.gip).toBe(undefined)
expect(data.appGip).toBe(undefined)
})

it('should have gip in NEXT_DATA for page with getInitialProps', async () => {
indexPageContent = await fs.readFile(indexPage, 'utf8')
await fs.writeFile(
indexPage,
`
const Page = () => 'hi'
Page.getInitialProps = () => ({ hello: 'world' })
export default Page
`
)
const data = await getData()
expect(data.gip).toBe(true)
})

it('should have gip and appGip in NEXT_DATA for page with getInitialProps and _app with getInitialProps', async () => {
await fs.writeFile(
appPage,
`
const App = ({ Component, pageProps }) => <Component {...pageProps} />
App.getInitialProps = async (ctx) => {
let pageProps = {}
if (ctx.Component.getInitialProps) {
pageProps = await ctx.Component.getInitialProps(ctx.ctx)
}
return { pageProps }
}
export default App
`
)
const data = await getData()
expect(data.gip).toBe(true)
expect(data.appGip).toBe(true)
})

it('should only have appGip in NEXT_DATA for page without getInitialProps and _app with getInitialProps', async () => {
await fs.writeFile(indexPage, indexPageContent)
const data = await getData()
await fs.remove(appPage)
expect(data.gip).toBe(undefined)
expect(data.appGip).toBe(true)
})
}

describe('gip identifiers', () => {
describe('dev mode', () => {
runTests(true)
})

describe('production mode', () => {
runTests()
})
})