{"id":18788121,"url":"https://github.com/thdk/nextjs-envs-demo","last_synced_at":"2025-12-24T17:30:13.478Z","repository":{"id":178103251,"uuid":"659435799","full_name":"thdk/nextjs-envs-demo","owner":"thdk","description":null,"archived":false,"fork":false,"pushed_at":"2023-07-03T17:32:37.000Z","size":56,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-12-29T13:48:27.707Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/thdk.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-06-27T20:44:07.000Z","updated_at":"2023-06-28T08:10:37.000Z","dependencies_parsed_at":null,"dependency_job_id":"ad4b36f8-9d57-421e-a281-25d54a9938f4","html_url":"https://github.com/thdk/nextjs-envs-demo","commit_stats":null,"previous_names":["thdk/nextjs-envs-demo"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thdk%2Fnextjs-envs-demo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thdk%2Fnextjs-envs-demo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thdk%2Fnextjs-envs-demo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thdk%2Fnextjs-envs-demo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thdk","download_url":"https://codeload.github.com/thdk/nextjs-envs-demo/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239708991,"owners_count":19684165,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-07T20:57:35.859Z","updated_at":"2025-12-24T17:30:13.448Z","avatar_url":"https://github.com/thdk.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Using environment variables with nextjs\n\nInspired by https://www.saltycrane.com/blog/2021/04/buildtime-vs-runtime-environment-variables-nextjs-docker/\n\n## Setup\n\nCreate a dummy nextjs 12 app\n```sh\nnpx create-next-app@12 nextjs-envs-demo \u0026\u0026 cd nextjs-envs-demo \u0026\u0026 npm i next@12\n```\n\nLog two envrionment variables from with the render method of your App component:\n```js\n// _app.js\nfunction MyApp({ Component, pageProps }) {\n  console.log('FOO', process.env.FOO);\n  console.log('NEXT_PUBLIC_FOO', process.env.NEXT_PUBLIC_FOO);\n  return \u003cComponent {...pageProps} /\u003e\n}\n\nexport default MyApp\n```\n\n## SSG Pages (Static Site Generation)\n\nRun following commands and visit: [http://localhost:3000]\n\n| set env at | cmd | log in browser console | log in server terminal at build time |\n| --- | --- | --- | --- |\n| none |```npm run dev```| undefined |  |\n| both |```FOO=bar npm run dev```| undefined | bar (runtime server terminal)|\n| build time|```FOO=bar npm run build; npm run start```| undefined | bar |\n| run time|```npm run build; FOO=bar npm run start```| undefined |  |\n| both |```NEXT_PUBLIC_FOO=bar npm run dev```| bar | bar (runtime server terminal)|\n| build time |```NEXT_PUBLIC_FOO=bar npm run build; npm run start```| bar | bar |\n| run time |```npm run build; NEXT_PUBLIC_FOO=bar npm run start```| undefined |  |\n\n## SSR (Server Side Rendered)\n\n```js\n// pages/server.js\nexport function getServerSideProps() {\n    return {props: {}};\n}\n```\n\nRun following commands and visit: [http://localhost:3000/server]\n\n| set env at |  cmd | log in browser console | log in server terminal at run time |\n| --- | --- | --- | --- |\n| none |```npm run dev```| undefined | undefined |\n| both|```FOO=bar npm run dev```| undefined | bar |\n| build time |```FOO=bar npm run build; npm run start```| undefined | undefined |\n| run time |```npm run build; FOO=bar npm run start```| undefined | bar |\n| both |```NEXT_PUBLIC_FOO=bar npm run dev```| bar | bar |\n| build time |```NEXT_PUBLIC_FOO=bar npm run build; npm run start```| bar | bar |\n| run time |```npm run build; NEXT_PUBLIC_FOO=bar npm run start```| undefined | bar |\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthdk%2Fnextjs-envs-demo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthdk%2Fnextjs-envs-demo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthdk%2Fnextjs-envs-demo/lists"}