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 = { ...@@ -65,6 +65,7 @@ export type NEXT_DATA = {
assetPrefix?: string assetPrefix?: string
runtimeConfig?: { [key: string]: any } runtimeConfig?: { [key: string]: any }
nextExport?: boolean nextExport?: boolean
autoExport?: boolean
skeleton?: boolean skeleton?: boolean
dynamicIds?: string[] dynamicIds?: string[]
err?: Error & { statusCode?: number } err?: Error & { statusCode?: number }
......
...@@ -131,6 +131,7 @@ type RenderOpts = { ...@@ -131,6 +131,7 @@ type RenderOpts = {
dangerousAsPath: string dangerousAsPath: string
assetPrefix?: string assetPrefix?: string
err?: Error | null err?: Error | null
autoExport?: boolean
nextExport?: boolean nextExport?: boolean
skeleton?: boolean skeleton?: boolean
dev?: boolean dev?: boolean
...@@ -163,6 +164,7 @@ function renderDocument( ...@@ -163,6 +164,7 @@ function renderDocument(
assetPrefix, assetPrefix,
runtimeConfig, runtimeConfig,
nextExport, nextExport,
autoExport,
skeleton, skeleton,
dynamicImportsIds, dynamicImportsIds,
dangerousAsPath, dangerousAsPath,
...@@ -207,6 +209,7 @@ function renderDocument( ...@@ -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 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 runtimeConfig, // runtimeConfig if provided, otherwise don't sent in the resulting HTML
nextExport, // If this is a page exported by `next export` 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 skeleton, // If this is a skeleton page for experimentalPrerender
dynamicIds: dynamicIds:
dynamicImportsIds.length === 0 ? undefined : dynamicImportsIds, dynamicImportsIds.length === 0 ? undefined : dynamicImportsIds,
...@@ -255,8 +258,20 @@ export async function renderToHTML( ...@@ -255,8 +258,20 @@ export async function renderToHTML(
} = renderOpts } = renderOpts
await Loadable.preloadAll() // Make sure all dynamic imports are loaded 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) { if (dev) {
const { isValidElementType } = require('react-is') const { isValidElementType } = require('react-is')
...@@ -278,11 +293,6 @@ export async function renderToHTML( ...@@ -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) { if (isStaticPage) {
// remove query values except ones that will be set during export // remove query values except ones that will be set during export
query = { query = {
...@@ -292,12 +302,8 @@ export async function renderToHTML( ...@@ -292,12 +302,8 @@ export async function renderToHTML(
renderOpts.nextExport = true 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 (isSkeleton) renderOpts.nextExport = true
if (isAutoExport) renderOpts.autoExport = true
// @ts-ignore url will always be set // @ts-ignore url will always be set
const asPath: string = req.url const asPath: string = req.url
......
...@@ -427,6 +427,24 @@ describe('Production Usage', () => { ...@@ -427,6 +427,24 @@ describe('Production Usage', () => {
expect(script).not.toMatch(/runtimeConfig/) 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') { if (browserName === 'chrome') {
it('should add preload tags when Link prefetch prop is used', async () => { it('should add preload tags when Link prefetch prop is used', async () => {
const browser = await webdriver(appPort, '/prefetch') const browser = await webdriver(appPort, '/prefetch')
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
/* global jasmine */ /* global jasmine */
import webdriver from 'next-webdriver' import webdriver from 'next-webdriver'
import { join } from 'path' import { join } from 'path'
import cheerio from 'cheerio'
import { existsSync, readdirSync, readFileSync } from 'fs' import { existsSync, readdirSync, readFileSync } from 'fs'
import { import {
killApp, killApp,
...@@ -34,6 +35,24 @@ describe('Serverless', () => { ...@@ -34,6 +35,24 @@ describe('Serverless', () => {
expect(html).toMatch(/Hello World/) 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 () => { it('should serve file from public folder', async () => {
const content = await renderViaHTTP(appPort, '/hello.txt') const content = await renderViaHTTP(appPort, '/hello.txt')
expect(content.trim()).toBe('hello world') 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