diff --git a/packages/next/server/next-dev-server.ts b/packages/next/server/next-dev-server.ts index 97b9c7ecd9bcf2..14dde763658d0a 100644 --- a/packages/next/server/next-dev-server.ts +++ b/packages/next/server/next-dev-server.ts @@ -482,10 +482,16 @@ export default class DevServer extends Server { // from waiting on them for the page to load in dev mode const __getStaticPaths = async () => { + const { publicRuntimeConfig, serverRuntimeConfig } = this.nextConfig + const paths = await this.staticPathsWorker.loadStaticPaths( this.distDir, pathname, - !this.renderOpts.dev && this._isLikeServerless + !this.renderOpts.dev && this._isLikeServerless, + { + publicRuntimeConfig, + serverRuntimeConfig, + } ) return paths } diff --git a/packages/next/server/static-paths-worker.ts b/packages/next/server/static-paths-worker.ts index 029c39e5543ab7..77da28a2c4b384 100644 --- a/packages/next/server/static-paths-worker.ts +++ b/packages/next/server/static-paths-worker.ts @@ -2,6 +2,8 @@ import { buildStaticPaths } from '../build/utils' import { loadComponents } from '../next-server/server/load-components' import '../next-server/server/node-polyfill-fetch' +type RuntimeConfig = any + let workerWasUsed = false // we call getStaticPaths in a separate process to ensure @@ -10,7 +12,8 @@ let workerWasUsed = false export async function loadStaticPaths( distDir: string, pathname: string, - serverless: boolean + serverless: boolean, + config: RuntimeConfig ) { // we only want to use each worker once to prevent any invalid // caches @@ -18,6 +21,9 @@ export async function loadStaticPaths( process.exit(1) } + // update work memory runtime-config + require('./../next-server/lib/runtime-config').setConfig(config) + const components = await loadComponents(distDir, pathname, serverless) if (!components.getStaticPaths) { diff --git a/test/integration/development-runtime-config/components/Layout.js b/test/integration/development-runtime-config/components/Layout.js new file mode 100644 index 00000000000000..75cc04d8d65dd5 --- /dev/null +++ b/test/integration/development-runtime-config/components/Layout.js @@ -0,0 +1,15 @@ +import getConfig from 'next/config' + +const { serverRuntimeConfig, publicRuntimeConfig } = getConfig() + +const Layout = ({ children }) => { + return ( +
+

{JSON.stringify(serverRuntimeConfig)}

+

{JSON.stringify(publicRuntimeConfig)}

+
{children}
+
+ ) +} + +export default Layout diff --git a/test/integration/development-runtime-config/pages/_app.js b/test/integration/development-runtime-config/pages/_app.js new file mode 100644 index 00000000000000..f76402f3025c8d --- /dev/null +++ b/test/integration/development-runtime-config/pages/_app.js @@ -0,0 +1,11 @@ +import Layout from './../components/Layout' + +const App = ({ Component, pageProps }) => { + return ( + + + + ) +} + +export default App diff --git a/test/integration/development-runtime-config/pages/index.js b/test/integration/development-runtime-config/pages/index.js new file mode 100644 index 00000000000000..f40492ac06d855 --- /dev/null +++ b/test/integration/development-runtime-config/pages/index.js @@ -0,0 +1 @@ +export default () => 'test' diff --git a/test/integration/development-runtime-config/pages/post/[pid].js b/test/integration/development-runtime-config/pages/post/[pid].js new file mode 100644 index 00000000000000..d5aa21f76fe99c --- /dev/null +++ b/test/integration/development-runtime-config/pages/post/[pid].js @@ -0,0 +1,22 @@ +const Post = () => { + return ( +
+

POST

+
+ ) +} + +export const getStaticProps = async () => { + return { + props: {}, + } +} + +export const getStaticPaths = async () => { + return { + paths: ['/post/1', '/post/2'], + fallback: true, + } +} + +export default Post diff --git a/test/integration/development-runtime-config/test/index.test.js b/test/integration/development-runtime-config/test/index.test.js new file mode 100644 index 00000000000000..942bfa4aa22a97 --- /dev/null +++ b/test/integration/development-runtime-config/test/index.test.js @@ -0,0 +1,79 @@ +/* eslint-env jest */ + +import fs from 'fs-extra' +import { join } from 'path' +import cheerio from 'cheerio' +import { + findPort, + renderViaHTTP, + launchApp, + waitFor, + killApp, +} from 'next-test-utils' + +jest.setTimeout(1000 * 60 * 2) + +const appDir = join(__dirname, '../') +const nextConfig = join(appDir, 'next.config.js') + +const runApp = async (config) => { + const port = await findPort() + + let stderr = '' + const app = await launchApp(appDir, port, { + onStderr(err) { + stderr += err + }, + }) + + const html = await renderViaHTTP(port, '/post/1') + const $ = cheerio.load(html) + await waitFor(1000) + + await killApp(app) + await fs.remove(nextConfig) + + expect(stderr).not.toMatch( + /Cannot read property 'serverRuntimeConfig' of undefined/i + ) + expect(JSON.parse($('#server-runtime-config').text())).toEqual( + config.serverRuntimeConfig || {} + ) + expect(JSON.parse($('#public-runtime-config').text())).toEqual( + config.publicRuntimeConfig || {} + ) +} + +describe('should work with runtime-config in next.config.js', () => { + test('empty runtime-config', async () => { + await fs.writeFile( + nextConfig, + ` + module.exports = { + } + ` + ) + + await runApp({}) + }) + + test('with runtime-config', async () => { + const config = { + serverRuntimeConfig: { + mySecret: '**********', + }, + publicRuntimeConfig: { + staticFolder: '/static', + }, + } + + await fs.writeFile( + nextConfig, + ` + module.exports = ${JSON.stringify(config)} + ` + ) + + await runApp(config) + }) +}) diff --git a/yarn.lock b/yarn.lock index d60891083747e6..d37237a85d4dcb 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4114,7 +4114,7 @@ browserify-zlib@^0.2.0: dependencies: pako "~1.0.5" -browserslist@4.13.0, browserslist@^4.0.0, browserslist@^4.11.1, browserslist@^4.13.0, browserslist@^4.3.6, browserslist@^4.6.4, browserslist@^4.8.3, browserslist@^4.8.5: +browserslist@4.13.0, browserslist@^1.3.6, browserslist@^1.5.2, browserslist@^1.7.6, browserslist@^4.0.0, browserslist@^4.11.1, browserslist@^4.13.0, browserslist@^4.3.6, browserslist@^4.6.4, browserslist@^4.8.3, browserslist@^4.8.5: version "4.13.0" resolved "https://registry.yarnpkg.com/browserslist/-/browserslist-4.13.0.tgz#42556cba011e1b0a2775b611cba6a8eca18e940d" integrity sha512-MINatJ5ZNrLnQ6blGvePd/QOz9Xtu+Ne+x29iQSCHfkU5BugKVJwZKn/iiL8UbpIpa3JhviKjz+XxMo0m2caFQ== @@ -4124,14 +4124,6 @@ browserslist@4.13.0, browserslist@^4.0.0, browserslist@^4.11.1, browserslist@^4. escalade "^3.0.1" node-releases "^1.1.58" -browserslist@^1.3.6, browserslist@^1.5.2, browserslist@^1.7.6: - version "1.7.7" - resolved "https://registry.yarnpkg.com/browserslist/-/browserslist-1.7.7.tgz#0bd76704258be829b2398bb50e4b62d1a166b0b9" - integrity sha1-C9dnBCWL6CmyOYu1Dkti0aFmsLk= - dependencies: - caniuse-db "^1.0.30000639" - electron-to-chromium "^1.2.7" - browserstack-local@1.4.0: version "1.4.0" resolved "https://registry.yarnpkg.com/browserstack-local/-/browserstack-local-1.4.0.tgz#d979cac056f57b9af159b3bcd7fdc09b4354537c" @@ -4461,21 +4453,11 @@ caniuse-db@^1.0.30000529, caniuse-db@^1.0.30000634: version "1.0.30001023" resolved "https://registry.yarnpkg.com/caniuse-db/-/caniuse-db-1.0.30001023.tgz#f856f71af16a5a44e81f1fcefc1673912a43da72" -caniuse-db@^1.0.30000639: - version "1.0.30001109" - resolved "https://registry.yarnpkg.com/caniuse-db/-/caniuse-db-1.0.30001109.tgz#756c5d689ce550c891e1456cf4c6b8f7a253ec19" - integrity sha512-B/DC85f77h55PRO9YTSyzMOA24CSAwzgBNCJtrTdEtejW/HGAn6kWgzNHLpU3fZFWWOdQ6ABo0YzvZ3BXPtqLw== - -caniuse-lite@^1.0.0, caniuse-lite@^1.0.30000981, caniuse-lite@^1.0.30001019, caniuse-lite@^1.0.30001020: +caniuse-lite@^1.0.0, caniuse-lite@^1.0.30000981, caniuse-lite@^1.0.30001019, caniuse-lite@^1.0.30001020, caniuse-lite@^1.0.30001093: version "1.0.30001066" resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001066.tgz#0a8a58a10108f2b9bf38e7b65c237b12fd9c5f04" integrity sha512-Gfj/WAastBtfxLws0RCh2sDbTK/8rJuSeZMecrSkNGYxPcv7EzblmDGfWQCFEQcSqYE2BRgQiJh8HOD07N5hIw== -caniuse-lite@^1.0.30001093: - version "1.0.30001109" - resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001109.tgz#a9f3f26a0c3753b063d7acbb48dfb9c0e46f2b19" - integrity sha512-4JIXRodHzdS3HdK8nSgIqXYLExOvG+D2/EenSvcub2Kp3QEADjo2v2oUn5g0n0D+UNwG9BtwKOyGcSq2qvQXvQ== - capitalize@1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/capitalize/-/capitalize-1.0.0.tgz#dc802c580aee101929020d2ca14b4ca8a0ae44be" @@ -6201,11 +6183,6 @@ ejs@^2.6.1: version "2.7.4" resolved "https://registry.yarnpkg.com/ejs/-/ejs-2.7.4.tgz#48661287573dcc53e366c7a1ae52c3a120eec9ba" -electron-to-chromium@^1.2.7: - version "1.3.516" - resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.516.tgz#03ec071b061e462b786bf7e7ce226fd7ab7cf1f6" - integrity sha512-WDM5AAQdOrvLqSX8g3Zd5AujBXfMxf96oeZkff0U2HF5op3tjShE+on2yay3r1UD4M9I3p0iHpAS4+yV8U8A9A== - electron-to-chromium@^1.3.488: version "1.3.501" resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.501.tgz#faa17a2cb0105ee30d5e1ca87eae7d8e85dd3175" @@ -7807,11 +7784,6 @@ he@1.1.1: resolved "https://registry.yarnpkg.com/he/-/he-1.1.1.tgz#93410fd21b009735151f8868c2f271f3427e23fd" integrity sha1-k0EP0hsAlzUVH4howvJx80J+I/0= -he@1.1.1: - version "1.1.1" - resolved "https://registry.yarnpkg.com/he/-/he-1.1.1.tgz#93410fd21b009735151f8868c2f271f3427e23fd" - integrity sha1-k0EP0hsAlzUVH4howvJx80J+I/0= - header-case@^1.0.0: version "1.0.1" resolved "https://registry.yarnpkg.com/header-case/-/header-case-1.0.1.tgz#9535973197c144b09613cd65d317ef19963bd02d"