Skip to content

Commit

Permalink
Add autoExport to __NEXT_DATA__ (#8746)
Browse files Browse the repository at this point in the history
* Add autoExport to __NEXT_DATA__

* Update render variable
  • Loading branch information
ijjk authored and timneutkens committed Sep 15, 2019
1 parent 00e0281 commit ed5dc21
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 12 deletions.
1 change: 1 addition & 0 deletions packages/next/next-server/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export type NEXT_DATA = {
assetPrefix?: string
runtimeConfig?: { [key: string]: any }
nextExport?: boolean
autoExport?: boolean
skeleton?: boolean
dynamicIds?: string[]
err?: Error & { statusCode?: number }
Expand Down
30 changes: 18 additions & 12 deletions packages/next/next-server/server/render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ type RenderOpts = {
dangerousAsPath: string
assetPrefix?: string
err?: Error | null
autoExport?: boolean
nextExport?: boolean
skeleton?: boolean
dev?: boolean
Expand Down Expand Up @@ -163,6 +164,7 @@ function renderDocument(
assetPrefix,
runtimeConfig,
nextExport,
autoExport,
skeleton,
dynamicImportsIds,
dangerousAsPath,
Expand Down Expand Up @@ -207,6 +209,7 @@ function renderDocument(
assetPrefix: assetPrefix === '' ? undefined : assetPrefix, // send assetPrefix to the client side when configured, otherwise don't sent in the resulting HTML
runtimeConfig, // runtimeConfig if provided, otherwise don't sent in the resulting HTML
nextExport, // If this is a page exported by `next export`
autoExport, // If this is an auto exported page
skeleton, // If this is a skeleton page for experimentalPrerender
dynamicIds:
dynamicImportsIds.length === 0 ? undefined : dynamicImportsIds,
Expand Down Expand Up @@ -255,8 +258,20 @@ export async function renderToHTML(
} = renderOpts

await Loadable.preloadAll() // Make sure all dynamic imports are loaded
let isStaticPage = pageConfig.experimentalPrerender === true
let isSkeleton = false

const defaultAppGetInitialProps =
App.getInitialProps === (App as any).origGetInitialProps

let isAutoExport =
typeof (Component as any).getInitialProps !== 'function' &&
defaultAppGetInitialProps

let isPrerender = pageConfig.experimentalPrerender === true
const isStaticPage = isPrerender || isAutoExport
// TODO: revisit `?_nextPreviewSkeleton=(truthy)`
const isSkeleton = isPrerender && !!query._nextPreviewSkeleton
// remove from query so it doesn't end up in document
delete query._nextPreviewSkeleton

if (dev) {
const { isValidElementType } = require('react-is')
Expand All @@ -278,11 +293,6 @@ export async function renderToHTML(
)
}

isStaticPage = typeof (Component as any).getInitialProps !== 'function'
const defaultAppGetInitialProps =
App.getInitialProps === (App as any).origGetInitialProps
isStaticPage = isStaticPage && defaultAppGetInitialProps

if (isStaticPage) {
// remove query values except ones that will be set during export
query = {
Expand All @@ -292,12 +302,8 @@ export async function renderToHTML(
renderOpts.nextExport = true
}
}
// might want to change previewing of skeleton from `?_nextPreviewSkeleton=(truthy)`
isSkeleton =
pageConfig.experimentalPrerender === true && !!query._nextPreviewSkeleton
// remove from query so it doesn't end up in document
delete query._nextPreviewSkeleton
if (isSkeleton) renderOpts.nextExport = true
if (isAutoExport) renderOpts.autoExport = true

// @ts-ignore url will always be set
const asPath: string = req.url
Expand Down
18 changes: 18 additions & 0 deletions test/integration/production/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,24 @@ describe('Production Usage', () => {
expect(script).not.toMatch(/runtimeConfig/)
})

it('should add autoExport for auto pre-rendered pages', async () => {
for (const page of ['/', '/about']) {
const html = await renderViaHTTP(appPort, page)
const $ = cheerio.load(html)
const data = JSON.parse($('#__NEXT_DATA__').html())
expect(data.autoExport).toBe(true)
}
})

it('should not add autoExport for non pre-rendered pages', async () => {
for (const page of ['/query']) {
const html = await renderViaHTTP(appPort, page)
const $ = cheerio.load(html)
const data = JSON.parse($('#__NEXT_DATA__').html())
expect(!!data.autoExport).toBe(false)
}
})

if (browserName === 'chrome') {
it('should add preload tags when Link prefetch prop is used', async () => {
const browser = await webdriver(appPort, '/prefetch')
Expand Down
19 changes: 19 additions & 0 deletions test/integration/serverless/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/* global jasmine */
import webdriver from 'next-webdriver'
import { join } from 'path'
import cheerio from 'cheerio'
import { existsSync, readdirSync, readFileSync } from 'fs'
import {
killApp,
Expand Down Expand Up @@ -34,6 +35,24 @@ describe('Serverless', () => {
expect(html).toMatch(/Hello World/)
})

it('should add autoExport for auto pre-rendered pages', async () => {
for (const page of ['/', '/abc']) {
const html = await renderViaHTTP(appPort, page)
const $ = cheerio.load(html)
const data = JSON.parse($('#__NEXT_DATA__').html())
expect(data.autoExport).toBe(true)
}
})

it('should not add autoExport for non pre-rendered pages', async () => {
for (const page of ['/fetch']) {
const html = await renderViaHTTP(appPort, page)
const $ = cheerio.load(html)
const data = JSON.parse($('#__NEXT_DATA__').html())
expect(!!data.autoExport).toBe(false)
}
})

it('should serve file from public folder', async () => {
const content = await renderViaHTTP(appPort, '/hello.txt')
expect(content.trim()).toBe('hello world')
Expand Down

0 comments on commit ed5dc21

Please sign in to comment.