question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Dynamic global styles with SSR issue

See original GitHub issue

Current behavior:

style tags generated by <Global /> components appear in front of style tags generated by SSR:

image

As a result, the dynamically calculated global styles cannot override the styles in the SSR:

image

To reproduce:

https://codesandbox.io/s/snowy-violet-tq704

Expected behavior:

The style generated by SSR should appear first:

image

Environment information:

  • react version: 17
  • @emotion/react version: 11

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:5
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
oliviertassinaricommented, Feb 16, 2021

The workaround proposed by @zce seems to do it.

    <React.Fragment>
      <Global
        styles={{
          body: {
            backgroundColor: darkmode ? "#000" : "#fff",
          },
        }}
      />
      <CacheProvider value={cache}>
        <Component {...pageProps} />
        <button onClick={() => setDarkMode((prevState) => !prevState)}>
          switch to {darkmode ? "light" : "dark"} mode!
        </button>
      </CacheProvider>
    </React.Fragment>

The only downside is one invalid HTML markup. It’s OK-ish for a temporary solution. The global style can be rendered before any other content of the body. The warning only happens once, not flooding / burying the other important potential errors too much. However, in the long term, we need to fix it.

Capture d’écran 2021-01-17 à 14 57 36

@Andarist We will keep it in mind, thanks for taking the time to share pointers on how you envision the solution. Maybe we can come back to it once we release v5 stable.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Implementing SSR in Next.js: Dynamic routing and prefetching
If you wanted to wrap the entire application in a component, perform some initialization operation, or include some global styles, then the _app ......
Read more >
Server Side Rendering - Emotion
If you have dynamic global styles it's advised to create cache per single render to avoid global styles from different renders leaking into...
Read more >
Using Non-SSR Friendly Components with Next.js
1. Use next/dynamic imports ... Next.js claims to support ES2020 dynamic import() for JavaScript. It allows you to import JavaScript modules dynamically and...
Read more >
Emotion 10: CSS-in-JS with Flexible Scoped and Global ...
A new Global component enables dynamic global styling. ... Class names containing Emotion styles from the className prop override css prop ...
Read more >
reactjs - Nextjs - Dynamic Imports - CSS Modules cannot be ...
I've never seen a "~" in an import path before, might that be causing a problem? – Mark Williams · @MarkWilliams I dont...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found