Skip to content
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
8 changes: 7 additions & 1 deletion packages/next/server/next-dev-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
8 changes: 7 additions & 1 deletion packages/next/server/static-paths-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -10,14 +12,18 @@ 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
if (workerWasUsed) {
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) {
Expand Down
15 changes: 15 additions & 0 deletions test/integration/development-runtime-config/components/Layout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import getConfig from 'next/config'

const { serverRuntimeConfig, publicRuntimeConfig } = getConfig()

const Layout = ({ children }) => {
return (
<div>
<p id="server-runtime-config">{JSON.stringify(serverRuntimeConfig)}</p>
<p id="public-runtime-config">{JSON.stringify(publicRuntimeConfig)}</p>
<div>{children}</div>
</div>
)
}

export default Layout
11 changes: 11 additions & 0 deletions test/integration/development-runtime-config/pages/_app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Layout from './../components/Layout'

const App = ({ Component, pageProps }) => {
return (
<Layout>
<Component {...pageProps} />
</Layout>
)
}

export default App
1 change: 1 addition & 0 deletions test/integration/development-runtime-config/pages/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default () => 'test'
22 changes: 22 additions & 0 deletions test/integration/development-runtime-config/pages/post/[pid].js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const Post = () => {
return (
<div>
<h2>POST</h2>
</div>
)
}

export const getStaticProps = async () => {
return {
props: {},
}
}

export const getStaticPaths = async () => {
return {
paths: ['/post/1', '/post/2'],
fallback: true,
}
}

export default Post
79 changes: 79 additions & 0 deletions test/integration/development-runtime-config/test/index.test.js
Original file line number Diff line number Diff line change
@@ -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)
})
})
32 changes: 2 additions & 30 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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==
Expand All @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down