Unverified Commit 764edc51 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Add identifier to NEXT_DATA when custom server is used (#10869)

parent 9edafc64
...@@ -80,6 +80,7 @@ export type NEXT_DATA = { ...@@ -80,6 +80,7 @@ export type NEXT_DATA = {
err?: Error & { statusCode?: number } err?: Error & { statusCode?: number }
gsp?: boolean gsp?: boolean
gssp?: boolean gssp?: boolean
customServer?: boolean
} }
/** /**
......
...@@ -91,6 +91,7 @@ export type ServerConstructor = { ...@@ -91,6 +91,7 @@ export type ServerConstructor = {
*/ */
conf?: NextConfig conf?: NextConfig
dev?: boolean dev?: boolean
customServer?: boolean
} }
export default class Server { export default class Server {
...@@ -116,6 +117,7 @@ export default class Server { ...@@ -116,6 +117,7 @@ export default class Server {
hasCssMode: boolean hasCssMode: boolean
dev?: boolean dev?: boolean
previewProps: __ApiPreviewProps previewProps: __ApiPreviewProps
customServer?: boolean
} }
private compression?: Middleware private compression?: Middleware
private onErrorMiddleware?: ({ err }: { err: Error }) => Promise<void> private onErrorMiddleware?: ({ err }: { err: Error }) => Promise<void>
...@@ -136,6 +138,7 @@ export default class Server { ...@@ -136,6 +138,7 @@ export default class Server {
quiet = false, quiet = false,
conf = null, conf = null,
dev = false, dev = false,
customServer = true,
}: ServerConstructor = {}) { }: ServerConstructor = {}) {
this.dir = resolve(dir) this.dir = resolve(dir)
this.quiet = quiet this.quiet = quiet
...@@ -167,6 +170,7 @@ export default class Server { ...@@ -167,6 +170,7 @@ export default class Server {
buildId: this.buildId, buildId: this.buildId,
generateEtags, generateEtags,
previewProps: this.getPreviewProps(), previewProps: this.getPreviewProps(),
customServer: customServer === true ? true : undefined,
} }
// Only the `publicRuntimeConfig` key is exposed to the client side // Only the `publicRuntimeConfig` key is exposed to the client side
......
...@@ -187,6 +187,7 @@ function renderDocument( ...@@ -187,6 +187,7 @@ function renderDocument(
headTags, headTags,
gsp, gsp,
gssp, gssp,
customServer,
}: RenderOpts & { }: RenderOpts & {
props: any props: any
docProps: DocumentInitialProps docProps: DocumentInitialProps
...@@ -209,6 +210,7 @@ function renderDocument( ...@@ -209,6 +210,7 @@ function renderDocument(
isFallback?: boolean isFallback?: boolean
gsp?: boolean gsp?: boolean
gssp?: boolean gssp?: boolean
customServer?: boolean
} }
): string { ): string {
return ( return (
...@@ -231,6 +233,7 @@ function renderDocument( ...@@ -231,6 +233,7 @@ function renderDocument(
err: err ? serializeError(dev, err) : undefined, // Error if one happened, otherwise don't sent in the resulting HTML err: err ? serializeError(dev, err) : undefined, // Error if one happened, otherwise don't sent in the resulting HTML
gsp, // whether the page is getStaticProps gsp, // whether the page is getStaticProps
gssp, // whether the page is getServerSideProps gssp, // whether the page is getServerSideProps
customServer, // whether the user is using a custom server
}, },
dangerousAsPath, dangerousAsPath,
canonicalBase, canonicalBase,
......
...@@ -6,7 +6,10 @@ export default async function start( ...@@ -6,7 +6,10 @@ export default async function start(
port?: number, port?: number,
hostname?: string hostname?: string
) { ) {
const app = next(serverOptions) const app = next({
...serverOptions,
customServer: false,
})
const srv = http.createServer(app.getRequestHandler()) const srv = http.createServer(app.getRequestHandler())
await new Promise((resolve, reject) => { await new Promise((resolve, reject) => {
// This code catches EADDRINUSE error if the port is already in use // This code catches EADDRINUSE error if the port is already in use
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
import webdriver from 'next-webdriver' import webdriver from 'next-webdriver'
import { join } from 'path' import { join } from 'path'
import getPort from 'get-port' import getPort from 'get-port'
import cheerio from 'cheerio'
import clone from 'clone' import clone from 'clone'
import { import {
initNextServerScript, initNextServerScript,
...@@ -89,6 +90,12 @@ describe('Custom Server', () => { ...@@ -89,6 +90,12 @@ describe('Custom Server', () => {
const html = await renderViaHTTP(appPort, '/dashboard') const html = await renderViaHTTP(appPort, '/dashboard')
expect(html).toMatch(/made it to dashboard/) expect(html).toMatch(/made it to dashboard/)
}) })
it('should contain customServer in NEXT_DATA', async () => {
const html = await renderViaHTTP(appPort, '/')
const $ = cheerio.load(html)
expect(JSON.parse($('#__NEXT_DATA__').text()).customServer).toBe(true)
})
}) })
describe('with generateEtags enabled', () => { describe('with generateEtags enabled', () => {
......
...@@ -239,6 +239,14 @@ describe('Production Usage', () => { ...@@ -239,6 +239,14 @@ describe('Production Usage', () => {
expect(html).toMatch(/404/) expect(html).toMatch(/404/)
} }
}) })
it('should not contain customServer in NEXT_DATA', async () => {
const html = await renderViaHTTP(appPort, '/')
const $ = cheerio.load(html)
expect('customServer' in JSON.parse($('#__NEXT_DATA__').text())).toBe(
false
)
})
}) })
describe('API routes', () => { describe('API routes', () => {
......
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