Skip to content
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

Overlay causes useLayoutEffect warnings in SSR mode #1453

Closed
jclem opened this issue Sep 22, 2021 · 5 comments · Fixed by #1583
Closed

Overlay causes useLayoutEffect warnings in SSR mode #1453

jclem opened this issue Sep 22, 2021 · 5 comments · Fixed by #1583
Assignees
Labels
bug Something isn't working react

Comments

@jclem
Copy link
Contributor

jclem commented Sep 22, 2021

Describe the bug

When using the <Overlay /> and components that build upon it in SSR mode, React emits warnings about calling useLayoutEffect during SSR.

Of course, there are legitimate uses of useLayoutEffect in Primer and cases where it may be unavoidable. However, in <Overlay />, it looks like this particular call is not doing any DOM measurement or direct manipulation, but is kicking off an animation. This seems like it's probably fine to defer until after a paint occurs, and I'm guessing that browser is probably going to do that anyway before this animation begins.

Given that, I wonder if we can move this to a useEffect call, instead or alternatively investigate using a useIsomorphicLayoutEffect, which @mattcosta7 reminded me about in Slack:

const useIsomorphicLayoutEffect = typeof document !== 'undefined' ? useLayoutEffect : useEffect

(That said, in this case, I think just using the simpler useEffect is fine.)

In order to avoid these warnings currently, I have to defer completely the rendering of overlays until we're on the client.

To Reproduce
Steps to reproduce the behavior:

  1. Create a Next.js app
  2. Render an <Overlay /> on some page
  3. Watch logs
@colebemis colebemis added the bug Something isn't working label Sep 23, 2021
@pksjce pksjce self-assigned this Sep 30, 2021
@pksjce
Copy link
Collaborator

pksjce commented Oct 6, 2021

I agree with @jclem here because there is no obvious reason for why we need useLayoutEffect.
I wonder though, Overlay component is rendered conditionally after click of a button, so it doesn't make sense that this is rendered during SSR.

Here's what I tried,

  • Created a new nextjs app.
  • Copied the Dialog Overlay story from Overlay stories
  • Added a getServerSideProps function on my page

Apart from an error that made me add ThemeProvider(this was not clear at all), I didn't get any warnings.

When I removed the conditional from rendering logic for Overlay in my component, I saw this warning,
Warning: useLayoutEffect does nothing on the server, because its effect cannot be encoded into the server renderer's output format. This will lead to a mismatch between the initial, non-hydrated UI and the intended UI. To avoid this, useLayoutEffect should only be used in components that render exclusively on the client. See https://reactjs.org/link/uselayouteffect-ssr for common fixes.
Also the UI completely failed with
error - ReferenceError: document is not defined at Portal (/Users/pk/Github/nextjs-primer-react/node_modules/@primer/components/lib/Portal/Portal.js:66:23)

So, I'm curious about how your bug and nextjs app looks like. Ideally I'd expect nothing from Overlay to run on SSR.

@jclem
Copy link
Contributor Author

jclem commented Oct 12, 2021

I'll have to go back a bit and find a commit this was still happening. Sorry for the delay, but I should be able to get to it in the next couple of days.

@gaknoll gaknoll added the react label Oct 19, 2021
@jclem
Copy link
Contributor Author

jclem commented Oct 26, 2021

I was wrong, it's not from Overlay, it's from AnchoredOverlay. It looks like for us, this was coming from AnchoredOverlay's useAnchoredPosition call here. Inside useAnchoredPosition, there is a call to useLayoutEffect here.

@siddharthkp siddharthkp self-assigned this Nov 1, 2021
@pksjce
Copy link
Collaborator

pksjce commented Nov 3, 2021

Was able to reproduce this here
https://github.com/pksjce/nextjs-primer-react

Looking at ways to mitigate this!

@pksjce
Copy link
Collaborator

pksjce commented Nov 3, 2021

Linking to this exploration by @siddharthkp https://github.com/pksjce/nextjs-primer-react/pull/1/files
I will look at going in direction 2 today and if not fallback to useIsomorphicEffect and have a PR up

@pksjce pksjce mentioned this issue Nov 8, 2021
6 tasks
@ghost ghost mentioned this issue Oct 13, 2022
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working react
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants