-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ReferenceError: navigator is not defined (remix react) #2368
Comments
I encountered a similar error when using the GraphiQL component on next.js. |
Hey @Hansanghyeon 👋 rendering GraphiQL server-side is currently to supported as @MH4GF correctly pointed out. To work around this in any React app you can also do something like this (basically exploiting the fact that function ClientOnlyGraphiQL() {
const [isClientRendering, setIsClientRendering] = useState(false);
useEffect(() => {
setIsClientRendering(true);
}, []);
return isClientRendering ? <GraphiQL /> : null;
} |
I have exactly the same issue in my remix app. I cannot upgrade above "graphiql": "1.7.2" and "graphiql-explorer": "0.9.0" because of codemirror and navigator. The solution above works up to those versions and then afterwards it doesn't. |
This has always been the case with GraphiQL unfortunately, as codemirror 5 is not designed for SSR and we have no control over that. For some time we had codemirror 5 working with next.js naturally (why we implemented inline require/dynamic import for codemirror imports) but several years ago it stopped working in nextjs out of the box, and folks had to use the didMount hack to avoid the node rendering step |
Yes we shouldn’t use any browser APIs except after did mount or inside useEffect(), though I guess node SSR servers go that far in the component lifecycle now? |
Because this version is the first that stopped working - [email protected]- I think for some reason #2249 is to blame. |
But to be clear, since the PR i introduced in 2018, GraphiQL has tried it‘s best to support SSR, and continue to do so however. In fact I think we supported SSR back when we still had the pre-webpack, gulp era toolchain! The timeout workaround should at least always work as a baseline. |
@AndKiel I just tried it out and can confirm that remix doesn't even start the dev server when you try to import something from graphiql/packages/graphiql-react/src/editor/common.ts Lines 20 to 32 in 4ad0793
All this stuff is dynamically imported when you import anything from |
@AndKiel ahhh!! Well i made that change so that esbuild folks could build the project without inline require. SIGH. Probably next.js trying to automatically prefetch dynamic imports or something. Well maybe I’ll try out the latest next.js here soon and see what I can come up with. But either way, it will involve only rendering the component client side, unless someone here has a magical way to render codemirror 5 on the server! |
@thomasheyenbrock wait, where are we importing codemirror modules top level? The file you linked to shows dynamic imports, which means they are only imported when the promise is resolved |
Not 100% sure what happens here (I'm not a bundler expert 😅 ), but what I did in #2545 fixed the Remix dev server that I used to replicate the issue locally. |
To clear up wording ambiguity, instead of "top-level imports" I should have said "top-level dynamic imports". These promises to get fired when importing the module, by putting them inside the |
I understand now, it’s because the imports weren’t in a closure! E.g. why we can do so another approach, we can still specify this config at the top level if we want, but it would be a function that is called that returns the array const addons = () => [import('something')]
function MyComponent() {
const addonPromisesToResolve = addons()
} sorry for the shitty iPhone psuedocode lol i am personally a fan of keeping configuration highly visible and towards the top of the file, but I’m old school so please disregard |
The fix was just published with |
implement a workaround to prevent the cursor from jumping to the beginning of the query window when typing quickly this fix comes from: graphql/graphiql#2368 (comment) possibly doesn't work all the time, GitHub issue is not being worked on as GraphiQL is looking to reimplement the Explorer plugin soon
Hi There,
I am trying to use GraphiQL with Hansanghyeon/error-remix-graphiql but I am getting following error:
There was a similar issue. #118
The text was updated successfully, but these errors were encountered: