Commit ed5dc210 authored by JJ Kasper's avatar JJ Kasper Committed by Tim Neutkens

Add autoExport to __NEXT_DATA__ (#8746)

* Add autoExport to __NEXT_DATA__

* Update render variable
parent 00e0281d
......@@ -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 }
......
......@@ -131,6 +131,7 @@ type RenderOpts = {
dangerousAsPath: string
assetPrefix?: string
err?: Error | null
autoExport?: boolean
nextExport?: boolean
skeleton?: boolean
dev?: boolean
......@@ -163,6 +164,7 @@ function renderDocument(
assetPrefix,
runtimeConfig,
nextExport,
autoExport,
skeleton,
dynamicImportsIds,
dangerousAsPath,
......@@ -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,
......@@ -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')
......@@ -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 = {
......@@ -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
......
......@@ -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')
......
......@@ -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,
......@@ -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')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment