Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to error on usage of serverRuntimeConfig with serverless #9030

Merged
merged 3 commits into from
Oct 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions errors/serverless-publicRuntimeConfig.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Using `publicRuntimeConfig` with `target` set to `serverless`
# Using `publicRuntimeConfig` or `serverRuntimeConfig` with `target` set to `serverless`

#### 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

Expand Down
2 changes: 1 addition & 1 deletion packages/next/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2045,7 +2045,7 @@ AuthMethod({ key: process.env.CUSTOM_KEY, secret: process.env.CUSTOM_SECRET })

#### 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.
> The reason for this is that runtime configuration adds rendering / initialization overhead and is **incompatible with [automatic static optimization](#automatic-static-optimization)**.
Expand Down
8 changes: 5 additions & 3 deletions packages/next/next-server/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,14 @@ export default function loadConfig(
if (
userConfig.target &&
userConfig.target !== 'server' &&
userConfig.publicRuntimeConfig &&
Object.keys(userConfig.publicRuntimeConfig).length !== 0
((userConfig.publicRuntimeConfig &&
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"
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'
)
}

Expand Down
10 changes: 6 additions & 4 deletions packages/next/next-server/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,12 @@ export default class Server {
}

// Initialize next/config with the environment configuration
envConfig.setConfig({
serverRuntimeConfig,
publicRuntimeConfig,
})
if (this.nextConfig.target === 'server') {
envConfig.setConfig({
serverRuntimeConfig,
publicRuntimeConfig,
})
}

const routes = this.generateRoutes()
this.router = new Router(routes)
Expand Down
1 change: 1 addition & 0 deletions test/integration/serverless-runtime-configs/pages/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default () => 'hi'
50 changes: 50 additions & 0 deletions test/integration/serverless-runtime-configs/test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* 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/
)
})
})