From 7a7386067631b20ee8c831932f5fb3635b7a951a Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Thu, 10 Oct 2019 15:31:08 -0500 Subject: [PATCH 1/3] Update to error on usage of serverRuntimeConfig with serverless --- errors/serverless-publicRuntimeConfig.md | 4 ++-- packages/next/next-server/server/config.ts | 8 +++++--- packages/next/next-server/server/next-server.ts | 10 ++++++---- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/errors/serverless-publicRuntimeConfig.md b/errors/serverless-publicRuntimeConfig.md index deb4ca5cecd93..800a1e664049a 100644 --- a/errors/serverless-publicRuntimeConfig.md +++ b/errors/serverless-publicRuntimeConfig.md @@ -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 diff --git a/packages/next/next-server/server/config.ts b/packages/next/next-server/server/config.ts index d2dce598349dd..60f3ac43758eb 100644 --- a/packages/next/next-server/server/config.ts +++ b/packages/next/next-server/server/config.ts @@ -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' ) } diff --git a/packages/next/next-server/server/next-server.ts b/packages/next/next-server/server/next-server.ts index c7e18d36becc1..dac09e2b3dabc 100644 --- a/packages/next/next-server/server/next-server.ts +++ b/packages/next/next-server/server/next-server.ts @@ -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) From 4f0f5ceface3eea8f1a167bceac1c7e5a03e5c9b Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Thu, 10 Oct 2019 15:39:11 -0500 Subject: [PATCH 2/3] Add tests for errors for serverless and runtime configs --- .../serverless-runtime-configs/pages/index.js | 1 + .../test/index.test.js | 50 +++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 test/integration/serverless-runtime-configs/pages/index.js create mode 100644 test/integration/serverless-runtime-configs/test/index.test.js diff --git a/test/integration/serverless-runtime-configs/pages/index.js b/test/integration/serverless-runtime-configs/pages/index.js new file mode 100644 index 0000000000000..0957a987fc2f2 --- /dev/null +++ b/test/integration/serverless-runtime-configs/pages/index.js @@ -0,0 +1 @@ +export default () => 'hi' diff --git a/test/integration/serverless-runtime-configs/test/index.test.js b/test/integration/serverless-runtime-configs/test/index.test.js new file mode 100644 index 0000000000000..d14e636352721 --- /dev/null +++ b/test/integration/serverless-runtime-configs/test/index.test.js @@ -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/ + ) + }) +}) From 50f06289f2ef9cd48f7a1775b046f89c8f32b9f1 Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Thu, 10 Oct 2019 15:43:50 -0500 Subject: [PATCH 3/3] Update docs wording --- packages/next/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/next/README.md b/packages/next/README.md index e210949d17b71..ee60d588f133a 100644 --- a/packages/next/README.md +++ b/packages/next/README.md @@ -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)**.