Allow Different Implementation for Function at Build Time depending on whether Function will be Run on the Client or the Server #63821
rweisman-cube
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Goals
Non-Goals
Background
What I'm Trying to Accomplish
geolocation
on theheaders
, but that requires importingnext/headers
, which is only available from the server-side, e.g. something like:Desired Behavior
In order for this to work consistently:
The Blocker
fetch
with this function doesn't work because any client code calling it will fail to build due to the firstimport
typeof window === 'undefined'
trick doesn't work as the application never passes the build phase...accomplishing the above requires a different implementation for the client vs the server.
There are a lot of assumptions here and my understanding of Vercel's environment is still evolving - there's probably an easier way to accomplish what I want to accomplish, but I can't find one. Does anybody have any thoughts?
Proposal
One of:
package.json
typeof window === 'undefined'
as works for runtimeexpress
, possibly a similar approach could be takenAlternate approaches:
next/headers
from the client-side with the understanding that it returns no dataBeta Was this translation helpful? Give feedback.
All reactions