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.

Wrong styles applied during SSR hydration

See original GitHub issue

Styled components is applying wrong styles during SSR hydration. The problem is that styles in production builds are swapped across components. For instance, the class sc-16rshtk is applied to a ToolbarContainer but contains the styles of a FooterLink. I’m using gatsby with gatsby-theme-material-ui and gatsby-plugin-styled-components.

This didn’t happen for client side rendering, only for SSR. Since it only affected components styled with styled-components, I tried to collect more information by assigning a displayName to each component, but it did nothing, classnames remained the same.

Then I found #261 and saw this comment. It says that styled-components should not be re-exported to make displayName work.

import styled from '../lib/styled-components'; //  Does not work :(
import styled from 'styled-components'; //  Works

After applying that change, I made a new production build and then discovered that SSR hydration was now working correctly. I suspect that babel-plugin-styled-components does not work if the package is re-exported.

This issue was very hard to debug.

If you were re-exporting the module to add theme type definitions, please use module augmentation instead.

// src/types/styled-components.ts
import type { Theme } from '@material-ui/core';

declare module 'styled-components' {
  export interface DefaultTheme extends Theme {
  }
}

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
mnajdovacommented, Nov 25, 2021
0reactions
javierturycommented, Dec 29, 2021

Are you reporting a bug or just confusion?

Now just confusion. Although at first I didn’t know about topLevelImportsPath and I thought it was a bug.

Also I would like to draw attention to how painful it was to find out that re-exporting the package was breaking SSR. Perhaps the debugging experience could be improved somehow or the documentation for the plugin could include a reference to topLevelImportsPath.

Did @mnajdova’s comment help?

I replicated the original problem in my project, used @mnajdova topLevelImportsPath to solve it and it worked.

Read more comments on GitHub >

github_iconTop Results From Across the Web

SSR with styled-components - when app.js loads site missing ...
However, when the app.js is loaded and hydrated together with SSR version, page is missing some styles and few elements breaks.
Read more >
Keeping Server-Side Rendering Cool With React Hydration
The purpose of this article is to share some helpful things to keep in mind to render a seamless experience as a Server-Side...
Read more >
Hydration and Server-side Rendering - somewhat abstract
It is this hydration that makes SSR worthwhile. There are two big rules to hydrating an application in React. The initial render cycle...
Read more >
The Perils of Rehydration - Josh W Comeau
This deep-dive tutorial examines how React and Gatsby can be used to pre-render ... My React component was rendering in the wrong spot!...
Read more >
Understanding Hydration in React applications(SSR)
Hydration is used only with server-side rendering (SSR). It is a process of rendering our components and attaching event handlers.
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