Unverified Commit 7627eb87 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Use publicRuntimeConfig for universal-configuration-runtime example (#5101)

parent 777aab2b
......@@ -41,4 +41,6 @@ now
## The idea behind the example
This example show how to set custom environment variables for your application at runtime
This example show how to set custom environment variables for your application at runtime using the `publicRuntimeConfig` key in `next.config.js`
For documentation see: https://github.com/zeit/next.js#exposing-configuration-to-the-server--client-side
export default (typeof window !== 'undefined' ? window.__ENV__ : process.env)
module.exports = {
publicRuntimeConfig: {
API_URL: process.env.API_URL
}
}
......@@ -5,7 +5,6 @@
"start": "next start"
},
"dependencies": {
"htmlescape": "1.1.1",
"next": "latest",
"react": "16.2.0",
"react-dom": "16.2.0"
......
import Document_, { Head, Main, NextScript } from 'next/document'
import htmlescape from 'htmlescape'
const { API_URL } = process.env
const env = { API_URL }
export default class Document extends Document_ {
static async getInitialProps (ctx) {
const props = await Document_.getInitialProps(ctx)
return props
}
render () {
return (
<html>
<Head />
<body>
<Main />
<script
dangerouslySetInnerHTML={{ __html: '__ENV__ = ' + htmlescape(env) }}
/>
<NextScript />
</body>
</html>
)
}
}
import React from 'react'
import env from '../lib/env'
import getConfig from 'next/config'
const {API_URL} = env
const {publicRuntimeConfig} = getConfig()
const {API_URL} = publicRuntimeConfig
export default class extends React.Component {
static async getInitialProps () {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment