mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
Implement new next/config keys (#3909)
This commit is contained in:
parent
a39e54c675
commit
802e879d33
|
@ -39,7 +39,10 @@ __webpack_public_path__ = `${assetPrefix}/_next/webpack/` //eslint-disable-line
|
||||||
// Initialize next/asset with the assetPrefix
|
// Initialize next/asset with the assetPrefix
|
||||||
asset.setAssetPrefix(assetPrefix)
|
asset.setAssetPrefix(assetPrefix)
|
||||||
// Initialize next/config with the environment configuration
|
// Initialize next/config with the environment configuration
|
||||||
envConfig.setConfig(runtimeConfig || {})
|
envConfig.setConfig({
|
||||||
|
serverRuntimeConfig: {},
|
||||||
|
publicRuntimeConfig: runtimeConfig
|
||||||
|
})
|
||||||
|
|
||||||
const asPath = getURL()
|
const asPath = getURL()
|
||||||
|
|
||||||
|
|
19
readme.md
19
readme.md
|
@ -1150,11 +1150,11 @@ The `config` key allows for exposing runtime configuration in your app. All keys
|
||||||
```js
|
```js
|
||||||
// next.config.js
|
// next.config.js
|
||||||
module.exports = {
|
module.exports = {
|
||||||
runtimeConfig: {
|
serverRuntimeConfig: { // Will only be available on the server side
|
||||||
mySecret: 'secret',
|
mySecret: 'secret'
|
||||||
public: {
|
},
|
||||||
staticFolder: '/static'
|
publicRuntimeConfig: { // Will be available on both server and client
|
||||||
}
|
staticFolder: '/static'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@ -1162,12 +1162,13 @@ module.exports = {
|
||||||
```js
|
```js
|
||||||
// pages/index.js
|
// pages/index.js
|
||||||
import getConfig from 'next/config'
|
import getConfig from 'next/config'
|
||||||
const config = getConfig()
|
const {serverRuntimeConfig, publicRuntimeConfig} = getConfig()
|
||||||
console.log(config.mySecret) // Will be 'secret' on the server, `undefined` on the client
|
|
||||||
console.log(config.public.staticFolder) // Will be '/static' on both server and client
|
console.log(serverRuntimeConfig.mySecret) // Will only be available on the server side
|
||||||
|
console.log(publicRuntimeConfig.staticFolder) // Will be available on both server and client
|
||||||
|
|
||||||
export default () => <div>
|
export default () => <div>
|
||||||
<img src={`${config.public.staticFolder}/logo.png`} />
|
<img src={`${publicRuntimeConfig.staticFolder}/logo.png`} />
|
||||||
</div>
|
</div>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
@ -98,20 +98,17 @@ export default async function (dir, options, configuration) {
|
||||||
availableChunks: getAvailableChunks(dir, nextConfig.distDir)
|
availableChunks: getAvailableChunks(dir, nextConfig.distDir)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Allow configuration from next.config.js to be passed to the server / client
|
const {serverRuntimeConfig, publicRuntimeConfig} = nextConfig
|
||||||
if (nextConfig.runtimeConfig) {
|
|
||||||
// Initialize next/config with the environment configuration
|
|
||||||
envConfig.setConfig(nextConfig.runtimeConfig)
|
|
||||||
|
|
||||||
// Only the `public` key is exposed to the client side
|
if (publicRuntimeConfig) {
|
||||||
// It'll be rendered as part of __NEXT_DATA__ on the client side
|
renderOpts.runtimeConfig = publicRuntimeConfig
|
||||||
if (nextConfig.runtimeConfig.public) {
|
|
||||||
renderOpts.runtimeConfig = {
|
|
||||||
public: nextConfig.runtimeConfig.public
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
envConfig.setConfig({
|
||||||
|
serverRuntimeConfig,
|
||||||
|
publicRuntimeConfig
|
||||||
|
})
|
||||||
|
|
||||||
// set the assetPrefix to use for 'next/asset'
|
// set the assetPrefix to use for 'next/asset'
|
||||||
setAssetPrefix(renderOpts.assetPrefix)
|
setAssetPrefix(renderOpts.assetPrefix)
|
||||||
|
|
||||||
|
|
|
@ -63,21 +63,21 @@ export default class Server {
|
||||||
availableChunks: dev ? {} : getAvailableChunks(this.dir, this.dist)
|
availableChunks: dev ? {} : getAvailableChunks(this.dir, this.dist)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Allow configuration from next.config.js to be passed to the server / client
|
const {serverRuntimeConfig, publicRuntimeConfig, assetPrefix} = this.nextConfig
|
||||||
if (this.nextConfig.runtimeConfig) {
|
|
||||||
// Initialize next/config with the environment configuration
|
|
||||||
envConfig.setConfig(this.nextConfig.runtimeConfig)
|
|
||||||
|
|
||||||
// Only the `public` key is exposed to the client side
|
// Only the `publicRuntimeConfig` key is exposed to the client side
|
||||||
// It'll be rendered as part of __NEXT_DATA__ on the client side
|
// It'll be rendered as part of __NEXT_DATA__ on the client side
|
||||||
if (this.nextConfig.runtimeConfig.public) {
|
if (publicRuntimeConfig) {
|
||||||
this.renderOpts.runtimeConfig = {
|
this.renderOpts.runtimeConfig = publicRuntimeConfig
|
||||||
public: this.nextConfig.runtimeConfig.public
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.setAssetPrefix(this.nextConfig.assetPrefix)
|
// Initialize next/config with the environment configuration
|
||||||
|
envConfig.setConfig({
|
||||||
|
serverRuntimeConfig,
|
||||||
|
publicRuntimeConfig
|
||||||
|
})
|
||||||
|
|
||||||
|
this.setAssetPrefix(assetPrefix)
|
||||||
this.defineRoutes()
|
this.defineRoutes()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,10 +6,10 @@ module.exports = withCSS({
|
||||||
maxInactiveAge: 1000 * 60 * 60
|
maxInactiveAge: 1000 * 60 * 60
|
||||||
},
|
},
|
||||||
cssModules: true,
|
cssModules: true,
|
||||||
runtimeConfig: {
|
serverRuntimeConfig: {
|
||||||
mySecret: 'secret',
|
mySecret: 'secret'
|
||||||
public: {
|
},
|
||||||
staticFolder: '/static'
|
publicRuntimeConfig: {
|
||||||
}
|
staticFolder: '/static'
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
|
@ -1,8 +1,7 @@
|
||||||
// pages/index.js
|
|
||||||
import getConfig from 'next/config'
|
import getConfig from 'next/config'
|
||||||
const config = getConfig()
|
const {serverRuntimeConfig, publicRuntimeConfig} = getConfig()
|
||||||
|
|
||||||
export default () => <div>
|
export default () => <div>
|
||||||
<p id='server-only'>{config.mySecret}</p>
|
<p id='server-only'>{serverRuntimeConfig.mySecret}</p>
|
||||||
<p id='server-and-client'>{config.public.staticFolder}</p>
|
<p id='server-and-client'>{publicRuntimeConfig.staticFolder}</p>
|
||||||
</div>
|
</div>
|
||||||
|
|
Loading…
Reference in a new issue