未验证 提交 764edc51 编写于 作者: J JJ Kasper 提交者: GitHub

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

上级 9edafc64
......@@ -80,6 +80,7 @@ export type NEXT_DATA = {
err?: Error & { statusCode?: number }
gsp?: boolean
gssp?: boolean
customServer?: boolean
}
/**
......
......@@ -91,6 +91,7 @@ export type ServerConstructor = {
*/
conf?: NextConfig
dev?: boolean
customServer?: boolean
}
export default class Server {
......@@ -116,6 +117,7 @@ export default class Server {
hasCssMode: boolean
dev?: boolean
previewProps: __ApiPreviewProps
customServer?: boolean
}
private compression?: Middleware
private onErrorMiddleware?: ({ err }: { err: Error }) => Promise<void>
......@@ -136,6 +138,7 @@ export default class Server {
quiet = false,
conf = null,
dev = false,
customServer = true,
}: ServerConstructor = {}) {
this.dir = resolve(dir)
this.quiet = quiet
......@@ -167,6 +170,7 @@ export default class Server {
buildId: this.buildId,
generateEtags,
previewProps: this.getPreviewProps(),
customServer: customServer === true ? true : undefined,
}
// Only the `publicRuntimeConfig` key is exposed to the client side
......
......@@ -187,6 +187,7 @@ function renderDocument(
headTags,
gsp,
gssp,
customServer,
}: RenderOpts & {
props: any
docProps: DocumentInitialProps
......@@ -209,6 +210,7 @@ function renderDocument(
isFallback?: boolean
gsp?: boolean
gssp?: boolean
customServer?: boolean
}
): string {
return (
......@@ -231,6 +233,7 @@ function renderDocument(
err: err ? serializeError(dev, err) : undefined, // Error if one happened, otherwise don't sent in the resulting HTML
gsp, // whether the page is getStaticProps
gssp, // whether the page is getServerSideProps
customServer, // whether the user is using a custom server
},
dangerousAsPath,
canonicalBase,
......
......@@ -6,7 +6,10 @@ export default async function start(
port?: number,
hostname?: string
) {
const app = next(serverOptions)
const app = next({
...serverOptions,
customServer: false,
})
const srv = http.createServer(app.getRequestHandler())
await new Promise((resolve, reject) => {
// This code catches EADDRINUSE error if the port is already in use
......
......@@ -3,6 +3,7 @@
import webdriver from 'next-webdriver'
import { join } from 'path'
import getPort from 'get-port'
import cheerio from 'cheerio'
import clone from 'clone'
import {
initNextServerScript,
......@@ -89,6 +90,12 @@ describe('Custom Server', () => {
const html = await renderViaHTTP(appPort, '/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', () => {
......
......@@ -239,6 +239,14 @@ describe('Production Usage', () => {
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', () => {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册