Skip to content

fix: optional-chain import.meta.env to prevent SSR crash when externalized - #18

Merged
yogeshchoudhary147 merged 1 commit into
mainfrom
fix/import-meta-env-optional-chain
Aug 24, 2026
Merged

fix: optional-chain import.meta.env to prevent SSR crash when externalized#18
yogeshchoudhary147 merged 1 commit into
mainfrom
fix/import-meta-env-optional-chain

Conversation

@yogeshchoudhary147

Copy link
Copy Markdown
Contributor

Problem

Auth0Provider crashes on first render when the package is installed from npm and Vite externalizes it. The CJS build (via tsup) replaces import.meta with {}, so import.meta.env is undefined. Accessing undefined.VITE_AUTH0_DOMAIN throws a TypeError.

The same crash occurs in Node.js ESM SSR since import.meta.env is a Vite construct — not available in plain Node.js.

Workaround in the sample app: ssr: { noExternal: ["@auth0/auth0-react-router"] } in vite.config.ts.

Fix

Add ?. optional chaining to all import.meta.env accesses (11 sites) in Auth0Provider.tsx. When env is undefined, every access safely returns undefined instead of throwing — isSpa evaluates to false and the SSR provider renders normally.

Test

Added a regression test in ssr-provider.test.tsx that sets import.meta.env = undefined at runtime (simulating the externalized CJS scenario) and asserts Auth0Provider does not throw.

@yogeshchoudhary147
yogeshchoudhary147 force-pushed the fix/import-meta-env-optional-chain branch from 19ae50a to e0d3c97 Compare August 24, 2026 12:00
@yogeshchoudhary147
yogeshchoudhary147 merged commit 1fd4252 into main Aug 24, 2026
4 checks passed
@yogeshchoudhary147
yogeshchoudhary147 deleted the fix/import-meta-env-optional-chain branch August 24, 2026 12:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants