Commit bf7af1d1 authored by JJ Kasper's avatar JJ Kasper Committed by Joe Haddad

Add default values for runtime config (#7847)

* Add default values for runtime config

* Add test to ensure default values are set
for runtime config

* Only add runtimeConfig if it's not empty

* Only add runtimeConfig if it's not empty

* Simplify default value adding

* Reduce bundle size

* remove comment changes

* Fix typos

* Add test for runtimeConfig in __NEXT_DATA__
parent c28f4674
...@@ -167,11 +167,11 @@ type Send<T> = (body: T) => void ...@@ -167,11 +167,11 @@ type Send<T> = (body: T) => void
*/ */
export type NextApiResponse<T = any> = ServerResponse & { export type NextApiResponse<T = any> = ServerResponse & {
/** /**
* Send data `any` data in reponse * Send data `any` data in response
*/ */
send: Send<T> send: Send<T>
/** /**
* Send data `json` data in reponse * Send data `json` data in response
*/ */
json: Send<T> json: Send<T>
status: (statusCode: number) => NextApiResponse<T> status: (statusCode: number) => NextApiResponse<T>
......
...@@ -39,6 +39,8 @@ const defaultConfig: { [key: string]: any } = { ...@@ -39,6 +39,8 @@ const defaultConfig: { [key: string]: any } = {
documentMiddleware: false, documentMiddleware: false,
publicDirectory: false, publicDirectory: false,
}, },
serverRuntimeConfig: {},
publicRuntimeConfig: {},
} }
function assignDefaults(userConfig: { [key: string]: any }) { function assignDefaults(userConfig: { [key: string]: any }) {
...@@ -104,6 +106,12 @@ export default function loadConfig( ...@@ -104,6 +106,12 @@ export default function loadConfig(
: canonicalBase) || '' : canonicalBase) || ''
} }
if (userConfig.target === 'serverless' && userConfig.publicRuntimeConfig) {
throw new Error(
'Cannot use publicRuntimeConfig with target=serverless https://err.sh/zeit/next.js/serverless-publicRuntimeConfig'
)
}
return assignDefaults({ configOrigin: CONFIG_FILE, ...userConfig }) return assignDefaults({ configOrigin: CONFIG_FILE, ...userConfig })
} }
......
...@@ -118,7 +118,7 @@ export default class Server { ...@@ -118,7 +118,7 @@ export default class Server {
// Only the `publicRuntimeConfig` key is exposed to the client side // Only the `publicRuntimeConfig` key is exposed to the client side
// It'll be rendered as part of __NEXT_DATA__ on the client side // It'll be rendered as part of __NEXT_DATA__ on the client side
if (publicRuntimeConfig) { if (Object.keys(publicRuntimeConfig).length > 0) {
this.renderOpts.runtimeConfig = publicRuntimeConfig this.renderOpts.runtimeConfig = publicRuntimeConfig
} }
......
...@@ -201,11 +201,6 @@ export default async function build(dir: string, conf = null): Promise<void> { ...@@ -201,11 +201,6 @@ export default async function build(dir: string, conf = null): Promise<void> {
let result: CompilerResult = { warnings: [], errors: [] } let result: CompilerResult = { warnings: [], errors: [] }
if (target === 'serverless') { if (target === 'serverless') {
if (config.publicRuntimeConfig)
throw new Error(
'Cannot use publicRuntimeConfig with target=serverless https://err.sh/zeit/next.js/serverless-publicRuntimeConfig'
)
const clientResult = await runCompiler(configs[0]) const clientResult = await runCompiler(configs[0])
// Fail build if clientResult contains errors // Fail build if clientResult contains errors
if (clientResult.errors.length > 0) { if (clientResult.errors.length > 0) {
......
/* global location */
import React, { Suspense } from 'react' import React, { Suspense } from 'react'
import ReactDOM from 'react-dom' import ReactDOM from 'react-dom'
import HeadManager from './head-manager' import HeadManager from './head-manager'
...@@ -49,7 +50,7 @@ __webpack_public_path__ = `${prefix}/_next/` //eslint-disable-line ...@@ -49,7 +50,7 @@ __webpack_public_path__ = `${prefix}/_next/` //eslint-disable-line
// Initialize next/config with the environment configuration // Initialize next/config with the environment configuration
envConfig.setConfig({ envConfig.setConfig({
serverRuntimeConfig: {}, serverRuntimeConfig: {},
publicRuntimeConfig: runtimeConfig publicRuntimeConfig: runtimeConfig || {}
}) })
const asPath = getURL() const asPath = getURL()
...@@ -84,7 +85,7 @@ class Container extends React.Component { ...@@ -84,7 +85,7 @@ class Container extends React.Component {
// If it's a dynamic route or has a querystring // If it's a dynamic route or has a querystring
if ( if (
data.nextExport && data.nextExport &&
(isDynamicRoute(router.pathname) || window.location.search) (isDynamicRoute(router.pathname) || location.search)
) { ) {
// update query on mount for exported pages // update query on mount for exported pages
router.replace( router.replace(
...@@ -92,7 +93,7 @@ class Container extends React.Component { ...@@ -92,7 +93,7 @@ class Container extends React.Component {
'?' + '?' +
stringifyQs({ stringifyQs({
...router.query, ...router.query,
...parseQs(window.location.search.substr(1)) ...parseQs(location.search.substr(1))
}), }),
asPath asPath
) )
...@@ -104,7 +105,7 @@ class Container extends React.Component { ...@@ -104,7 +105,7 @@ class Container extends React.Component {
} }
scrollToHash () { scrollToHash () {
let { hash } = window.location let { hash } = location
hash = hash && hash.substring(1) hash = hash && hash.substring(1)
if (!hash) return if (!hash) return
...@@ -276,7 +277,7 @@ async function doRender ({ App, Component, props, err }) { ...@@ -276,7 +277,7 @@ async function doRender ({ App, Component, props, err }) {
appProps appProps
}) })
// In development runtime errors are caught by react-error-overlay. // In development runtime errors are caught by react-error-overlay
if (process.env.NODE_ENV === 'development') { if (process.env.NODE_ENV === 'development') {
renderReactElement( renderReactElement(
<AppContainer> <AppContainer>
...@@ -285,7 +286,7 @@ async function doRender ({ App, Component, props, err }) { ...@@ -285,7 +286,7 @@ async function doRender ({ App, Component, props, err }) {
appElement appElement
) )
} else { } else {
// In production we catch runtime errors using componentDidCatch which will trigger renderError. // In production we catch runtime errors using componentDidCatch which will trigger renderError
renderReactElement( renderReactElement(
<AppContainer> <AppContainer>
<App {...appProps} /> <App {...appProps} />
......
...@@ -110,7 +110,7 @@ export default async function (dir, options, configuration) { ...@@ -110,7 +110,7 @@ export default async function (dir, options, configuration) {
const { serverRuntimeConfig, publicRuntimeConfig } = nextConfig const { serverRuntimeConfig, publicRuntimeConfig } = nextConfig
if (publicRuntimeConfig) { if (Object.keys(publicRuntimeConfig).length > 0) {
renderOpts.runtimeConfig = publicRuntimeConfig renderOpts.runtimeConfig = publicRuntimeConfig
} }
......
import getConfig from 'next/config'
const page = () => {
const { publicRuntimeConfig, serverRuntimeConfig } = getConfig()
return (
<>
{publicRuntimeConfig && <p>found public config</p>}
{serverRuntimeConfig && <p>found server config</p>}
</>
)
}
page.getInitialProps = () => ({})
export default page
...@@ -375,6 +375,19 @@ describe('Production Usage', () => { ...@@ -375,6 +375,19 @@ describe('Production Usage', () => {
await browser.close() await browser.close()
}) })
it('should have default runtime values when not defined', async () => {
const html = await renderViaHTTP(appPort, '/runtime-config')
expect(html).toMatch(/found public config/)
expect(html).toMatch(/found server config/)
})
it('should not have runtimeConfig in __NEXT_DATA__', async () => {
const html = await renderViaHTTP(appPort, '/runtime-config')
const $ = cheerio.load(html)
const script = $('#__NEXT_DATA__').html()
expect(script).not.toMatch(/runtimeConfig/)
})
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')
......
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