diff --git a/docs/02-app/02-api-reference/05-next-config-js/output.mdx b/docs/02-app/02-api-reference/05-next-config-js/output.mdx index 02271017baec..4d57da48a679 100644 --- a/docs/02-app/02-api-reference/05-next-config-js/output.mdx +++ b/docs/02-app/02-api-reference/05-next-config-js/output.mdx @@ -39,7 +39,6 @@ Additionally, a minimal `server.js` file is also output which can be used instea > **Good to know**: > -> - `next.config.js` is read during `next build` and serialized into the `server.js` output file. If the legacy [`serverRuntimeConfig` or `publicRuntimeConfig` options](/docs/app/api-reference/next-config-js/runtime-configuration) are being used, the values will be specific to values at build time. > - If your project uses [Image Optimization](/docs/app/building-your-application/optimizing/images) with the default `loader`, you must install `sharp` as a dependency: > - If your project needs alternative port or hostname for listening, you can define `PORT` and `HOSTNAME` environment variables, before running `server.js`. For example, `PORT=3000 HOSTNAME=localhost node server.js`. diff --git a/docs/02-app/02-api-reference/05-next-config-js/runtime-configuration.mdx b/docs/02-app/02-api-reference/05-next-config-js/runtime-configuration.mdx deleted file mode 100644 index ddf4f02368d9..000000000000 --- a/docs/02-app/02-api-reference/05-next-config-js/runtime-configuration.mdx +++ /dev/null @@ -1,58 +0,0 @@ ---- -title: Runtime Config -description: Add client and server runtime configuration to your Next.js app. ---- - -{/* The content of this doc is shared between the app and pages router. You can use the `Content` component to add content that is specific to the Pages Router. Any shared content should not be wrapped in a component. */} - -> **Good to know**: 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. - -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 ( -
- logo -
- ) -} - -export default MyImage -``` diff --git a/docs/03-pages/02-api-reference/03-next-config-js/runtime-configuration.mdx b/docs/03-pages/02-api-reference/03-next-config-js/runtime-configuration.mdx index 066ca717ebb5..4e092594cd70 100644 --- a/docs/03-pages/02-api-reference/03-next-config-js/runtime-configuration.mdx +++ b/docs/03-pages/02-api-reference/03-next-config-js/runtime-configuration.mdx @@ -1,7 +1,56 @@ --- title: Runtime Config description: Add client and server runtime configuration to your Next.js app. -source: app/api-reference/next-config-js/runtime-configuration --- -{/* 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 `Content` component to add content that is specific to the Pages Router. Any shared content should not be wrapped in a component. */} +> **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. + +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 ( +
+ logo +
+ ) +} + +export default MyImage +```