Skip to content

Commit aee02f8

Browse files
authored
Revert "[Breaking] Remove deprecated publicRuntimeConfig and serverRuntimeConfig (#83944)" (#84167)
This PR reverts #83944 as it was missing a deprecation warning in the app, which is easy to miss for the users.
1 parent b1d412c commit aee02f8

File tree

48 files changed

+477
-12
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+477
-12
lines changed

crates/next-build-test/nextConfig.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@
5353
"productionBrowserSourceMaps": false,
5454
"optimizeFonts": true,
5555
"excludeDefaultMomentLocales": true,
56+
"serverRuntimeConfig": {},
57+
"publicRuntimeConfig": {},
5658
"reactProductionProfiling": false,
5759
"reactStrictMode": true,
5860
"httpAgentOptions": {

docs/01-app/02-guides/environment-variables.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ This allows you to use a singular Docker image that can be promoted through mult
228228
**Good to know:**
229229

230230
- You can run code on server startup using the [`register` function](/docs/app/guides/instrumentation).
231+
- We do not recommend using the [`runtimeConfig`](/docs/pages/api-reference/config/next-config-js/runtime-configuration) option, as this does not work with the standalone output mode. Instead, we recommend [incrementally adopting](/docs/app/guides/migrating/app-router-migration) the App Router if you need this feature.
231232

232233
## Test Environment Variables
233234

docs/01-app/02-guides/self-hosting.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ This allows you to use a singular Docker image that can be promoted through mult
7979
> **Good to know:**
8080
>
8181
> - You can run code on server startup using the [`register` function](/docs/app/guides/instrumentation).
82+
> - We do not recommend using the [runtimeConfig](/docs/pages/api-reference/config/next-config-js/runtime-configuration) option, as this does not work with the standalone output mode. Instead, we recommend [incrementally adopting](/docs/app/guides/migrating/app-router-migration) the App Router.
8283
8384
## Caching and ISR
8485

docs/01-app/03-api-reference/05-config/01-next-config-js/output.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ node .next/standalone/server.js
5959

6060
> **Good to know**:
6161
>
62-
> - `next.config.js` is read during `next build` and serialized into the `server.js` output file.
62+
> - `next.config.js` is read during `next build` and serialized into the `server.js` output file. If the legacy [`serverRuntimeConfig` or `publicRuntimeConfig` options](/docs/pages/api-reference/config/next-config-js/runtime-configuration) are being used, the values will be specific to values at build time.
6363
> - If your project needs to listen to a specific port or hostname, you can define `PORT` or `HOSTNAME` environment variables before running `server.js`. For example, run `PORT=8080 HOSTNAME=0.0.0.0 node server.js` to start the server on `http://0.0.0.0:8080`.
6464
6565
</PagesOnly>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
---
2+
title: Runtime Config
3+
description: Add client and server runtime configuration to your Next.js app.
4+
---
5+
6+
> **Warning:**
7+
>
8+
> - **This feature is deprecated.** We recommend using [environment variables](/docs/pages/guides/environment-variables) instead, which also can support reading runtime values.
9+
> - You can run code on server startup using the [`register` function](/docs/app/guides/instrumentation).
10+
> - This feature does not work with [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization), [Output File Tracing](/docs/pages/api-reference/config/next-config-js/output#automatically-copying-traced-files), or [React Server Components](/docs/app/getting-started/server-and-client-components).
11+
12+
To add runtime configuration to your app, open `next.config.js` and add the `publicRuntimeConfig` and `serverRuntimeConfig` configs:
13+
14+
```js filename="next.config.js"
15+
module.exports = {
16+
serverRuntimeConfig: {
17+
// Will only be available on the server side
18+
mySecret: 'secret',
19+
secondSecret: process.env.SECOND_SECRET, // Pass through env variables
20+
},
21+
publicRuntimeConfig: {
22+
// Will be available on both server and client
23+
staticFolder: '/static',
24+
},
25+
}
26+
```
27+
28+
Place any server-only runtime config under `serverRuntimeConfig`.
29+
30+
Anything accessible to both client and server-side code should be under `publicRuntimeConfig`.
31+
32+
> A page that relies on `publicRuntimeConfig` **must** use `getInitialProps` or `getServerSideProps` or your application must have a [Custom App](/docs/pages/building-your-application/routing/custom-app) with `getInitialProps` to opt-out of [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization). Runtime configuration won't be available to any page (or component in a page) without being server-side rendered.
33+
34+
To get access to the runtime configs in your app use `next/config`, like so:
35+
36+
```jsx
37+
import getConfig from 'next/config'
38+
import Image from 'next/image'
39+
40+
// Only holds serverRuntimeConfig and publicRuntimeConfig
41+
const { serverRuntimeConfig, publicRuntimeConfig } = getConfig()
42+
// Will only be available on the server-side
43+
console.log(serverRuntimeConfig.mySecret)
44+
// Will be available on both server-side and client-side
45+
console.log(publicRuntimeConfig.staticFolder)
46+
47+
function MyImage() {
48+
return (
49+
<div>
50+
<Image
51+
src={`${publicRuntimeConfig.staticFolder}/logo.png`}
52+
alt="logo"
53+
layout="fill"
54+
/>
55+
</div>
56+
)
57+
}
58+
59+
export default MyImage
60+
```

packages/next/config.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import getConfig from './dist/shared/lib/runtime-config.external'
2+
export * from './dist/shared/lib/runtime-config.external'
3+
export default getConfig

packages/next/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./dist/shared/lib/runtime-config.external')

packages/next/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
/// <reference path="./amp.d.ts" />
55
/// <reference path="./app.d.ts" />
66
/// <reference path="./cache.d.ts" />
7+
/// <reference path="./config.d.ts" />
78
/// <reference path="./document.d.ts" />
89
/// <reference path="./dynamic.d.ts" />
910
/// <reference path="./error.d.ts" />

packages/next/src/build/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1909,7 +1909,9 @@ export default async function build(
19091909
}
19101910
}
19111911

1912-
const { configFileName } = config
1912+
const { configFileName, publicRuntimeConfig, serverRuntimeConfig } =
1913+
config
1914+
const runtimeEnvConfig = { publicRuntimeConfig, serverRuntimeConfig }
19131915
const sriEnabled = Boolean(config.experimental.sri?.algorithm)
19141916

19151917
const nonStaticErrorPageSpan = staticCheckSpan.traceChild(
@@ -1922,6 +1924,7 @@ export default async function build(
19221924
(await worker.hasCustomGetInitialProps({
19231925
page: '/_error',
19241926
distDir,
1927+
runtimeEnvConfig,
19251928
checkingApp: false,
19261929
sriEnabled,
19271930
}))
@@ -1935,6 +1938,7 @@ export default async function build(
19351938
page: '/_error',
19361939
distDir,
19371940
configFileName,
1941+
runtimeEnvConfig,
19381942
cacheComponents: isAppCacheComponentsEnabled,
19391943
authInterrupts: isAuthInterruptsEnabled,
19401944
httpAgentOptions: config.httpAgentOptions,
@@ -1954,6 +1958,7 @@ export default async function build(
19541958
? worker.hasCustomGetInitialProps({
19551959
page: appPageToCheck,
19561960
distDir,
1961+
runtimeEnvConfig,
19571962
checkingApp: true,
19581963
sriEnabled,
19591964
})
@@ -1963,6 +1968,7 @@ export default async function build(
19631968
? worker.getDefinedNamedExports({
19641969
page: appPageToCheck,
19651970
distDir,
1971+
runtimeEnvConfig,
19661972
sriEnabled,
19671973
})
19681974
: Promise.resolve([])
@@ -2149,6 +2155,7 @@ export default async function build(
21492155
originalAppPath,
21502156
distDir,
21512157
configFileName,
2158+
runtimeEnvConfig,
21522159
httpAgentOptions: config.httpAgentOptions,
21532160
locales: config.i18n?.locales,
21542161
defaultLocale: config.i18n?.defaultLocale,

packages/next/src/build/templates/edge-ssr.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,12 @@ async function requestHandler(
155155
distDir: '',
156156
crossOrigin: nextConfig.crossOrigin ? nextConfig.crossOrigin : undefined,
157157
largePageDataBytes: nextConfig.experimental.largePageDataBytes,
158+
// Only the `publicRuntimeConfig` key is exposed to the client side
159+
// It'll be rendered as part of __NEXT_DATA__ on the client side
160+
runtimeConfig:
161+
Object.keys(nextConfig.publicRuntimeConfig).length > 0
162+
? nextConfig.publicRuntimeConfig
163+
: undefined,
158164

159165
isExperimentalCompile: nextConfig.experimental.isExperimentalCompile,
160166
// `htmlLimitedBots` is passed to server as serialized config in string format

0 commit comments

Comments
 (0)