-
Notifications
You must be signed in to change notification settings - Fork 4.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: verify environment variables #4115
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { createEnv } from '@t3-oss/env-nextjs' | ||
import { z } from 'zod' | ||
import { upperCase } from 'lodash-es' | ||
|
||
export const env = createEnv({ | ||
/** | ||
* Specify your server-side environment variables schema here. This way you can ensure the app | ||
* isn't built with invalid env vars. | ||
*/ | ||
server: { | ||
NODE_ENV: z | ||
.enum(['DEVELOPMENT', 'PRODUCTION']) | ||
.default('DEVELOPMENT'), | ||
}, | ||
|
||
/** | ||
* Specify your client-side environment variables schema here. This way you can ensure the app | ||
* isn't built with invalid env vars. To expose them to the client, prefix them with | ||
* `NEXT_PUBLIC_`. | ||
*/ | ||
client: { | ||
NEXT_PUBLIC_DEPLOY_ENV: z | ||
.enum(['DEVELOPMENT', 'PRODUCTION']), | ||
NEXT_PUBLIC_EDITION: z | ||
.enum(['SELF_HOSTED']), | ||
NEXT_PUBLIC_API_PREFIX: z.string().url(), | ||
NEXT_PUBLIC_PUBLIC_API_PREFIX: z.string().url(), | ||
NEXT_PUBLIC_SENTRY_DSN: z.string().url().url().optional(), | ||
NEXT_PUBLIC_NODE_ENV: z | ||
.enum(['DEVELOPMENT', 'PRODUCTION']) | ||
.default('DEVELOPMENT'), | ||
NEXT_PUBLIC_MAINTENANCE_NOTICE: z.boolean().optional(), | ||
NEXT_PUBLIC_SITE_ABOUT: z.boolean().optional(), | ||
}, | ||
|
||
/** | ||
* You can't destruct `process.env` as a regular object in the Next.js edge runtimes (e.g. | ||
* middlewares) or client-side so we need to destruct manually. | ||
*/ | ||
runtimeEnv: { | ||
NODE_ENV: upperCase(process.env.NODE_ENV), | ||
NEXT_PUBLIC_NODE_ENV: upperCase(process.env.NODE_ENV), | ||
NEXT_PUBLIC_DEPLOY_ENV: process.env.NEXT_PUBLIC_DEPLOY_ENV, | ||
NEXT_PUBLIC_EDITION: process.env.NEXT_PUBLIC_EDITION, | ||
NEXT_PUBLIC_API_PREFIX: process.env.NEXT_PUBLIC_API_PREFIX, | ||
NEXT_PUBLIC_PUBLIC_API_PREFIX: process.env.NEXT_PUBLIC_PUBLIC_API_PREFIX, | ||
NEXT_PUBLIC_SENTRY_DSN: process.env.NEXT_PUBLIC_SENTRY_DSN, | ||
NEXT_PUBLIC_MAINTENANCE_NOTICE: process.env.NEXT_PUBLIC_MAINTENANCE_NOTICE, | ||
NEXT_PUBLIC_SITE_ABOUT: process.env.NEXT_PUBLIC_MAINTENANCE_NOTICE, | ||
}, | ||
/** | ||
* Run `build` or `dev` with `SKIP_ENV_VALIDATION` to skip env validation. This is especially | ||
* useful for Docker builds. | ||
*/ | ||
skipValidation: !!process.env.SKIP_ENV_VALIDATION, | ||
/** | ||
* Makes it so that empty strings are treated as undefined. `SOME_VAR: z.string()` and | ||
* `SOME_VAR=''` will throw an error. | ||
*/ | ||
emptyStringAsUndefined: true, | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ | |
"lint": "next lint", | ||
"fix": "next lint --fix", | ||
"eslint-fix": "eslint --fix", | ||
"prepare": "cd ../ && node -e \"if (process.env.NODE_ENV !== 'production'){process.exit(1)} \" || husky install ./web/.husky", | ||
"prepare": "cp .env.example .env && cd ../ && node -e \"if (process.env.NODE_ENV !== 'production'){process.exit(1)} \" || husky install ./web/.husky", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will cause .env.example to overwrite .env after each installation. I need to think again There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use a script to solve the problem. When .evn already has the same key value as .env.example, skip the update to avoid being overwritten. |
||
"gen-icons": "node ./app/components/base/icons/script.js", | ||
"uglify-embed": "node ./bin/uglify-embed", | ||
"check-i18n": "node ./i18n/script.js" | ||
|
@@ -28,6 +28,7 @@ | |
"@next/mdx": "^14.0.4", | ||
"@sentry/react": "^7.54.0", | ||
"@sentry/utils": "^7.54.0", | ||
"@t3-oss/env-nextjs": "^0.10.1", | ||
"@tailwindcss/line-clamp": "^0.4.4", | ||
"@tailwindcss/typography": "^0.5.9", | ||
"ahooks": "^3.7.5", | ||
|
@@ -84,6 +85,7 @@ | |
"swr": "^2.1.0", | ||
"use-context-selector": "^1.4.1", | ||
"uuid": "^9.0.1", | ||
"zod": "^3.23.6", | ||
"zustand": "^4.5.1" | ||
}, | ||
"devDependencies": { | ||
|
@@ -112,11 +114,12 @@ | |
"eslint": "^8.36.0", | ||
"eslint-config-next": "^14.0.4", | ||
"husky": "^8.0.3", | ||
"jiti": "^1.21.0", | ||
"lint-staged": "^13.2.2", | ||
"postcss": "^8.4.31", | ||
"sass": "^1.61.0", | ||
"tailwindcss": "^3.3.3", | ||
"typescript": "4.9.5", | ||
"typescript": "^5.4.5", | ||
"uglify-js": "^3.17.4" | ||
}, | ||
"lint-staged": { | ||
|
@@ -130,4 +133,4 @@ | |
"engines": { | ||
"node": ">=18.17.0" | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Client components cannot get the env values w/o these relaying code. Zod validations also fail on the client side. Plz give it a full test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you share your testing method and screenshots of test results? There is no problem with my test here.
I have no problem outputting the variables in the NormalForm component.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have a question here🤔, is there any writing operation required for these variables?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think these only apply for the SaaS env.