|
| 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 | +``` |
0 commit comments