emotion-js / emotion

👩‍🎤 CSS-in-JS library designed for high performance style composition

Home Page:https://emotion.sh/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Browser leaves duplicated style node when using ssr

zombieJ opened this issue · comments

Current behavior:

Hydrate will not de-dup

截屏2021-09-10 下午5 26 53

To reproduce:

Create a styled component and put into page:

const App = () => (
  <Button />
  <Button />
  <Button />
  <Button />
  <Button />
);

Expected behavior:

It's OK to get duplicated style definition on Server render:
截屏2021-09-10 下午5 28 43

But it should de-dup when hydrate on client side.

Environment information:

  • react version: 17.0.2
  • @emotion/react version: 11.4.1

I think it may same as #2436

cache.sheet.hydrate(nodesToHydrate)

I guess hydrate logic do not dedup which makes too many same style keep in the page.

The problem, most likely, is that your components have no common Emotion-aware ancestor. Try wrapping the whole thing in the CacheProvider - it should help you.

Tried the CacheProvider. Didn't work.

Please always try to share a repro case in a runnable form - either by providing a git repository to clone or a codesandbox. OSS maintainers usually can't afford the time to set up the repro, even if exact steps are given.

Wrapping in CacheProvider has helped in my case:

const myCache = createCache({ key: "cache-key" });

function MyApp({ Component, pageProps }: AppProps) {
  return (
    <CacheProvider value={myCache}>
      <Component {...pageProps} />
    </CacheProvider>
  );
}

We had this problem with @emotion/react v11.9.0 but it seems to fixed in v11.9.3.