mirror of
https://github.com/hanzoai/next.js.git
synced 2026-09-24 17:00:51 +00:00
<img width="764" alt="CleanShot 2023-12-17 at 11 25 10@2x" src="https://github.com/vercel/next.js/assets/9113740/437a8a0d-0f56-4199-9629-b767569349de">
61 lines
2.5 KiB
Plaintext
61 lines
2.5 KiB
Plaintext
---
|
|
title: Runtime Config
|
|
description: Add client and server runtime configuration to your Next.js app.
|
|
---
|
|
|
|
> **Warning:**
|
|
>
|
|
> - **This feature is deprecated.** We recommend using [environment variables](/docs/pages/building-your-application/configuring/environment-variables) instead, which also can support reading runtime values.
|
|
> - You can run code on server startup using the [`register` function](/docs/app/building-your-application/optimizing/instrumentation).
|
|
> - 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/next-config-js/output#automatically-copying-traced-files), or [React Server Components](/docs/app/building-your-application/rendering/server-components).
|
|
|
|
To add runtime configuration to your app, open `next.config.js` and add the `publicRuntimeConfig` and `serverRuntimeConfig` configs:
|
|
|
|
```js filename="next.config.js"
|
|
module.exports = {
|
|
serverRuntimeConfig: {
|
|
// Will only be available on the server side
|
|
mySecret: 'secret',
|
|
secondSecret: process.env.SECOND_SECRET, // Pass through env variables
|
|
},
|
|
publicRuntimeConfig: {
|
|
// Will be available on both server and client
|
|
staticFolder: '/static',
|
|
},
|
|
}
|
|
```
|
|
|
|
Place any server-only runtime config under `serverRuntimeConfig`.
|
|
|
|
Anything accessible to both client and server-side code should be under `publicRuntimeConfig`.
|
|
|
|
> 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.
|
|
|
|
To get access to the runtime configs in your app use `next/config`, like so:
|
|
|
|
```jsx
|
|
import getConfig from 'next/config'
|
|
import Image from 'next/image'
|
|
|
|
// Only holds serverRuntimeConfig and publicRuntimeConfig
|
|
const { serverRuntimeConfig, publicRuntimeConfig } = getConfig()
|
|
// Will only be available on the server-side
|
|
console.log(serverRuntimeConfig.mySecret)
|
|
// Will be available on both server-side and client-side
|
|
console.log(publicRuntimeConfig.staticFolder)
|
|
|
|
function MyImage() {
|
|
return (
|
|
<div>
|
|
<Image
|
|
src={`${publicRuntimeConfig.staticFolder}/logo.png`}
|
|
alt="logo"
|
|
layout="fill"
|
|
/>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
export default MyImage
|
|
```
|