https://github.com/t3-oss/create-t3-app/tree/next/cli/template/base/src/env
Last active
December 2, 2022 01:59
-
-
Save VladSez/72bd7d8a66245eb7a872f8699a504bd3 to your computer and use it in GitHub Desktop.
Validate env in next.js https://github.com/t3-oss/create-t3-app/tree/next/cli/template/base https://create.t3.gg/en/usage/env-variables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @ts-check | |
/** | |
* Run `build` or `dev` with `SKIP_ENV_VALIDATION` to skip env validation. | |
* This is especially useful for Docker builds. | |
*/ | |
!process.env.SKIP_ENV_VALIDATION && (await import("./src/env/server.mjs")); | |
/** @type {import("next").NextConfig} */ | |
const config = { | |
reactStrictMode: true, | |
swcMinify: true, | |
i18n: { | |
locales: ["en"], | |
defaultLocale: "en", | |
}, | |
}; | |
export default config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @ts-check | |
/** | |
* This file is included in `/next.config.mjs` which ensures the app isn't built with invalid env vars. | |
* It has to be a `.mjs`-file to be imported there. | |
*/ | |
import { serverSchema } from "./schema.mjs"; | |
import { env as clientEnv, formatErrors } from "./client.mjs"; | |
const _serverEnv = serverSchema.safeParse(process.env); | |
if (!_serverEnv.success) { | |
console.error( | |
"❌ Invalid environment variables:\n", | |
...formatErrors(_serverEnv.error.format()), | |
); | |
throw new Error("Invalid environment variables"); | |
} | |
for (let key of Object.keys(_serverEnv.data)) { | |
if (key.startsWith("NEXT_PUBLIC_")) { | |
console.warn("❌ You are exposing a server-side env-variable:", key); | |
throw new Error("You are exposing a server-side env-variable"); | |
} | |
} | |
export const env = { ..._serverEnv.data, ...clientEnv }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment