Server component cannot see server side environment variables but can see vars marked NEXT_PUBLIC
Answered
Jamison posted this in #help-forum
JamisonOP
When I log the public version (for testing) of this environment variable it works fine but the server side version is undefined. I've accessed it before in a similar way in another server component. Here this component is a child of a client component... is this the issue? Any ideas appreciated 🙂
Answered by joulev
It is definitely being rendered as a client component.
Components are client components if and only if
* “use clientâ€, or
* they are imported to another client component
Components are client components if and only if
* “use clientâ€, or
* they are imported to another client component
8 Replies
@Jamison When I log the public version (for testing) of this environment variable it works fine but the server side version is undefined. I've accessed it before in a similar way in another server component. Here this component is a child of a client component... is this the issue? Any ideas appreciated 🙂
It is definitely being rendered as a client component.
Components are client components if and only if
* “use clientâ€, or
* they are imported to another client component
Components are client components if and only if
* “use clientâ€, or
* they are imported to another client component
Answer
JamisonOP
Oh... of course. Thanks
Wondering how I could hide this key in this component, might need to rethink my solution
@Jamison Wondering how I could hide this key in this component, might need to rethink my solution
Asian black bear
Move the component with fetching above the client parts as a "wrapper" then drill the queried data into the client side with props
JamisonOP
Okay sounds good I'll give it a shot
Asian black bear
Putting this in files that will definitely not work on the client makes debugging a lot easier https://www.npmjs.com/package/server-only
Because you dont have to suss out stuff like, oh the env variable is not set for some reason?!?
JamisonOP
wth didn't even know i had super reactions