You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+7-5Lines changed: 7 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -50,21 +50,23 @@ If `src/index.ssr.js` exports a function called `devServerHandler`, it will be i
50
50
### Relative Urls, PUBLIC_URL and BASE_HREF
51
51
`create-react-app` provides a variable called `PUBLIC_URL`, which is accessible via `process.env.PUBLIC_URL` in JavaScript or by using the placeholder `%PUBLIC_URL%` in HTML to reference assets. This variable is determined at **compile time** and must be specified before compilation, which makes your compiled web app dependant on the specified location.
52
52
53
-
For projects with server-side rendering however, there is another, possibly better way to deal with this. Instead of hardcoding the public url, HTML assets can be referenced with a relative url and the server-side renderer can read a specified base url at **runtime** and render it into a base tag. `react-scripts-with-ssr` supports using `PUBLIC_URL` but recommends the described approach using the `BASE_HREF`environment variable:
53
+
For projects with server-side rendering however, there is anotherway to deal with this. Instead of hardcoding the public url, HTML assets can be referenced with a relative url and the server-side renderer can read a specified base url at **runtime** and render it into a base tag. `react-scripts-with-ssr` supports using `PUBLIC_URL` but also allows the `BASE_HREF`approach:
54
54
* Define a `BASE_HREF` environment variable in your server runtime environment containing your base url (e.g. `https://example.com/folder`).
55
55
* Add `<base href="%BASE_HREF%/" />` to your index.html template. Notice the trailing slash.
56
56
* Replace `%BASE_HREF%` with the value of `process.env.BASE_HREF` in your server-side request handler.
57
57
* Make all asset references in HTML relative. Do not use `%PUBLIC_URL%` in URLs or server-relative URLs (e.g. `/some/url`).
58
58
* Make the build scripts emit relative paths by providing `PUBLIC_URL` as environment variable with the value `.` during compile time or setting the `homepage` field in `package.json` to `.`.
59
59
* If you need access to BASE_HREF in JavaScript, read it from `document.getElementsByTagName("base")[0].href;`.
60
60
61
-
### Using runtime environment variables on the client
61
+
### Environment variables
62
62
63
-
`create-react-app`provides `process.env` on the client-side to access environment variables prefixed with REACT_APP and defined at **compile time**.
63
+
`create-react-app`embeds environment variables prefixed with REACT_APP in the build during **compile time** and provides them with a simulated `process.env` object using Webpack's DefinePlugin.
64
64
65
-
However, if you want to make your server's **runtime** environment variables available on the client-side, you can do this with a `clientEnv` variable:
65
+
While embedding is necessary on the client-side, the ssr request handler runs in a Node.js context with real environment variables. `react-scripts-with-ssr` allows accessing external environment variables in the ssr request handler during **runtime**. However, all variables contained in the client-side build are also embedded into the ssr request handler as default values if they are not provided by the runtime environment.
66
+
67
+
If you want to make your server's **runtime** environment variables available on the client-side, you can do this with a `clientEnv` variable:
66
68
* Add a variable assignment script `<script>clientEnv=%CLIENT_ENV%</script>` to the head section in `public/index.html`.
67
-
* Replace the placeholder in the ssr request handler with `.replace(/%CLIENT_ENV%/g, JSON.stringify({ FOO: 'bar' }))`.
69
+
* Replace the placeholder in the ssr request handler with `.replace(/%CLIENT_ENV%/g, JSON.stringify({ FOO: process.env.FOO }))`.
68
70
* Access the value on the client-side with `clientEnv.FOO`.
69
71
* For TypeScript: Declare the global variable `declare const clientEnv: NodeJS.ProcessEnv;`.
0 commit comments