🚸 Skip validation if __ENV.js file does not exist
This commit is contained in:
18
packages/env/env.ts
vendored
18
packages/env/env.ts
vendored
@ -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}`
|
||||
|
5
packages/env/getRuntimeVariable.ts
vendored
5
packages/env/getRuntimeVariable.ts
vendored
@ -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]
|
||||
}
|
||||
|
Reference in New Issue
Block a user