|
1 | 1 | --- |
2 | 2 | title: Runtime Config |
3 | 3 | description: Add client and server runtime configuration to your Next.js app. |
4 | | -source: app/api-reference/next-config-js/runtime-configuration |
5 | 4 | --- |
6 | 5 |
|
7 | | -{/* DO NOT EDIT. The content of this doc is generated from the source above. To edit the content of this page, navigate to the source page in your editor. You can use the `<PagesOnly>Content</PagesOnly>` component to add content that is specific to the Pages Router. Any shared content should not be wrapped in a component. */} |
| 6 | +> **Warning**: This feature is considered legacy and does not work with [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization), [Output File Tracing](/docs/pages/api-reference/next-config-js/output#automatically-copying-traced-files), or [React Server Components](/docs/getting-started/react-essentials#server-components). Please use [environment variables](/docs/pages/building-your-application/configuring/environment-variables) instead to avoid initialization overhead. |
| 7 | +
|
| 8 | +To add runtime configuration to your app, open `next.config.js` and add the `publicRuntimeConfig` and `serverRuntimeConfig` configs: |
| 9 | + |
| 10 | +```js filename="next.config.js" |
| 11 | +module.exports = { |
| 12 | + serverRuntimeConfig: { |
| 13 | + // Will only be available on the server side |
| 14 | + mySecret: 'secret', |
| 15 | + secondSecret: process.env.SECOND_SECRET, // Pass through env variables |
| 16 | + }, |
| 17 | + publicRuntimeConfig: { |
| 18 | + // Will be available on both server and client |
| 19 | + staticFolder: '/static', |
| 20 | + }, |
| 21 | +} |
| 22 | +``` |
| 23 | + |
| 24 | +Place any server-only runtime config under `serverRuntimeConfig`. |
| 25 | + |
| 26 | +Anything accessible to both client and server-side code should be under `publicRuntimeConfig`. |
| 27 | + |
| 28 | +> 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. |
| 29 | +
|
| 30 | +To get access to the runtime configs in your app use `next/config`, like so: |
| 31 | + |
| 32 | +```jsx |
| 33 | +import getConfig from 'next/config' |
| 34 | +import Image from 'next/image' |
| 35 | + |
| 36 | +// Only holds serverRuntimeConfig and publicRuntimeConfig |
| 37 | +const { serverRuntimeConfig, publicRuntimeConfig } = getConfig() |
| 38 | +// Will only be available on the server-side |
| 39 | +console.log(serverRuntimeConfig.mySecret) |
| 40 | +// Will be available on both server-side and client-side |
| 41 | +console.log(publicRuntimeConfig.staticFolder) |
| 42 | + |
| 43 | +function MyImage() { |
| 44 | + return ( |
| 45 | + <div> |
| 46 | + <Image |
| 47 | + src={`${publicRuntimeConfig.staticFolder}/logo.png`} |
| 48 | + alt="logo" |
| 49 | + layout="fill" |
| 50 | + /> |
| 51 | + </div> |
| 52 | + ) |
| 53 | +} |
| 54 | + |
| 55 | +export default MyImage |
| 56 | +``` |
0 commit comments