2
0

🚸 Skip validation if __ENV.js file does not exist

This commit is contained in:
Baptiste Arnaud
2023-08-31 09:57:54 +02:00
parent 9d29a88ed3
commit dfcfdf2138
9 changed files with 61 additions and 99 deletions

18
packages/env/env.ts vendored
View File

@ -1,5 +1,5 @@
import { createEnv } from '@t3-oss/env-nextjs'
import { ZodError, z } from 'zod'
import { z } from 'zod'
import { getRuntimeVariable } from './getRuntimeVariable'
declare const window: {
@ -317,21 +317,7 @@ export const env = createEnv({
...sentryEnv.runtimeEnv,
...posthogEnv.runtimeEnv,
},
onValidationError: (error: ZodError) => {
console.log(
'[DEBUG]',
"typeof window !== 'undefined'",
typeof window !== 'undefined'
)
if (typeof window !== 'undefined') {
console.log('[DEBUG]', 'window.__ENV', window.__ENV)
}
console.error(
'❌ Invalid environment variables:',
error.flatten().fieldErrors
)
throw new Error('Invalid environment variables')
},
skipValidation: typeof window !== 'undefined' && window.__ENV === undefined,
onInvalidAccess: (variable: string) => {
throw new Error(
`❌ Attempted to access a server-side environment variable on the client: ${variable}`

View File

@ -2,10 +2,9 @@ declare const window: {
__ENV?: any
}
const isBrowser = () => Boolean(typeof window !== 'undefined' && window.__ENV)
export const getRuntimeVariable = (key: string) => {
if (isBrowser()) return window.__ENV[key]
if (typeof window !== 'undefined')
return window.__ENV ? window.__ENV[key] : undefined
if (typeof process === 'undefined') return undefined
return process.env[key]
}