Unverified Commit 5b574fc7 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Update to error on usage of serverRuntimeConfig with serverless (#9030)

* Update to error on usage of serverRuntimeConfig with serverless

* Add tests for errors for serverless and runtime configs

* Update docs wording
parent 9ff7339e
# Using `publicRuntimeConfig` with `target` set to `serverless` # Using `publicRuntimeConfig` or `serverRuntimeConfig` with `target` set to `serverless`
#### Why This Error Occurred #### Why This Error Occurred
In the `serverless` target environment `next.config.js` is not loaded, so we don't support `publicRuntimeConfig`. In the `serverless` target environment `next.config.js` is not loaded, so we don't support `publicRuntimeConfig` or `serverRuntimeConfig`.
#### Possible Ways to Fix It #### Possible Ways to Fix It
......
...@@ -2045,7 +2045,7 @@ AuthMethod({ key: process.env.CUSTOM_KEY, secret: process.env.CUSTOM_SECRET }) ...@@ -2045,7 +2045,7 @@ AuthMethod({ key: process.env.CUSTOM_KEY, secret: process.env.CUSTOM_SECRET })
#### Runtime configuration #### Runtime configuration
> **Warning:** Note that this option is not available when using `target: 'serverless'` > **Warning:** Note that these options are not available when using `target: 'serverless'`
> **Warning:** Generally you want to use build-time configuration to provide your configuration. > **Warning:** Generally you want to use build-time configuration to provide your configuration.
> The reason for this is that runtime configuration adds rendering / initialization overhead and is **incompatible with [automatic static optimization](#automatic-static-optimization)**. > The reason for this is that runtime configuration adds rendering / initialization overhead and is **incompatible with [automatic static optimization](#automatic-static-optimization)**.
......
...@@ -147,12 +147,14 @@ export default function loadConfig( ...@@ -147,12 +147,14 @@ export default function loadConfig(
if ( if (
userConfig.target && userConfig.target &&
userConfig.target !== 'server' && userConfig.target !== 'server' &&
userConfig.publicRuntimeConfig && ((userConfig.publicRuntimeConfig &&
Object.keys(userConfig.publicRuntimeConfig).length !== 0 Object.keys(userConfig.publicRuntimeConfig).length !== 0) ||
(userConfig.serverRuntimeConfig &&
Object.keys(userConfig.serverRuntimeConfig).length !== 0))
) { ) {
// TODO: change error message tone to "Only compatible with [fat] server mode" // TODO: change error message tone to "Only compatible with [fat] server mode"
throw new Error( throw new Error(
'Cannot use publicRuntimeConfig with target=serverless https://err.sh/zeit/next.js/serverless-publicRuntimeConfig' 'Cannot use publicRuntimeConfig or serverRuntimeConfig with target=serverless https://err.sh/zeit/next.js/serverless-publicRuntimeConfig'
) )
} }
......
...@@ -143,10 +143,12 @@ export default class Server { ...@@ -143,10 +143,12 @@ export default class Server {
} }
// Initialize next/config with the environment configuration // Initialize next/config with the environment configuration
envConfig.setConfig({ if (this.nextConfig.target === 'server') {
serverRuntimeConfig, envConfig.setConfig({
publicRuntimeConfig, serverRuntimeConfig,
}) publicRuntimeConfig,
})
}
const routes = this.generateRoutes() const routes = this.generateRoutes()
this.router = new Router(routes) this.router = new Router(routes)
......
/* eslint-env jest */
/* global jasmine */
import fs from 'fs-extra'
import { join } from 'path'
import { nextBuild } from 'next-test-utils'
const appDir = join(__dirname, '../')
const nextConfigPath = join(appDir, 'next.config.js')
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 2
const cleanUp = () => fs.remove(nextConfigPath)
describe('Serverless runtime configs', () => {
beforeAll(() => cleanUp())
afterAll(() => cleanUp())
it('should error on usage of publicRuntimeConfig', async () => {
await fs.writeFile(
nextConfigPath,
`module.exports = {
target: 'serverless',
publicRuntimeConfig: {
hello: 'world'
}
}`
)
const { stderr } = await nextBuild(appDir, undefined, { stderr: true })
expect(stderr).toMatch(
/Cannot use publicRuntimeConfig or serverRuntimeConfig/
)
})
it('should error on usage of serverRuntimeConfig', async () => {
await fs.writeFile(
nextConfigPath,
`module.exports = {
target: 'serverless',
serverRuntimeConfig: {
hello: 'world'
}
}`
)
const { stderr } = await nextBuild(appDir, undefined, { stderr: true })
expect(stderr).toMatch(
/Cannot use publicRuntimeConfig or serverRuntimeConfig/
)
})
})
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