Skip to content

Commit 21e7705

Browse files
authored
Merge pull request #27 from nemanjam/feature/runtime-env-article
Next.js runtime environment variables article
2 parents 9524428 + cd47590 commit 21e7705

5 files changed

Lines changed: 635 additions & 0 deletions

File tree

docs/working-notes/todo4.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,3 +216,38 @@ tailwind 4, eslint 9
216216
reuse and clean up mpc, opi, rpi scripts // impossible
217217
--------
218218
decent Astro, ShadCN example https://github.com/shadcnblocks/mainline-astro-template
219+
-------------
220+
load env.js from nginx
221+
{isProd && <script src="/env.js" />}
222+
```yml
223+
nmc-nginx-with-volume:
224+
image: nginx:1.29.1-alpine3.22-slim
225+
container_name: nmc-nginx-with-volume
226+
restart: unless-stopped
227+
volumes:
228+
- ./website:/usr/share/nginx/html
229+
- ./env.js:/usr/share/nginx/html/env.js
230+
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
231+
```
232+
need to include <script /> wherever i use env vars
233+
must do all in js, not html
234+
```html
235+
<!-- Load your runtime env first -->
236+
<script src="/env.js"></script>
237+
238+
<!-- Partytown script -->
239+
<script>
240+
// wait until env.js is loaded
241+
const script = document.createElement("script");
242+
script.defer = true;
243+
script.type = "text/partytown";
244+
245+
// dynamically set attributes from runtime env
246+
script.src = window.__RUNTIME_ENV__.PLAUSIBLE_SCRIPT_URL;
247+
script.dataset.domain = window.__RUNTIME_ENV__.PLAUSIBLE_DOMAIN;
248+
249+
document.head.appendChild(script);
250+
</script>
251+
```
252+
bash sed replace much better, envsubst instead sed for env vars
253+
custom command: in nginx docker-compose.yml too
28.4 KB
Loading

src/content/post/2025/12-13-nextjs-runtime-environment-variables/_resources/index-devto.md

Whitespace-only changes.

src/content/post/2025/12-13-nextjs-runtime-environment-variables/_resources/index-hashnode.md

Whitespace-only changes.

0 commit comments

Comments
 (0)