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

gatsby-plugin-styled-components: Styles not updating #28849

Closed
tmatek opened this issue Jan 5, 2021 · 3 comments
Closed

gatsby-plugin-styled-components: Styles not updating #28849

tmatek opened this issue Jan 5, 2021 · 3 comments
Labels
stale? Issue that may be closed soon due to the original author not responding any more. topic: plugins-styling Relates to the styling (e.g. CSS-in-JS, SASS, PostCSS) type: bug An issue or pull request relating to a bug in Gatsby

Comments

@tmatek
Copy link

tmatek commented Jan 5, 2021

Description

I am not sure if this is an actual bug with styled-components' SSR or just my misunderstanding of how rehydration works. Conditionally applying styles to a button, based on the presence of a query param does not work in production (styles are not applied). It works in development.

Steps to reproduce

Pull down the repository at https://github.com/tmatek/gatsby-styled-components-bug.git. Run npm i && npm run build && npm run serve. Go to http://localhost:9000/test/?disable=1. Inspect the button element for CSS rules.

Expected result

The button should have opacity: 0.5; cursor: not-allowed; CSS rules applied.

Actual result

The button does not have the CSS rules applied. The console log shows that the conditional part in the button styled component was called.

Environment

  System:
    OS: macOS 10.15.2
    CPU: (4) x64 Intel(R) Core(TM) i5-5257U CPU @ 2.70GHz
    Shell: 3.2.57 - /bin/bash
  Binaries:
    Node: 10.23.0 - /usr/local/bin/node
    Yarn: 1.22.4 - /usr/local/bin/yarn
    npm: 6.14.8 - /usr/local/bin/npm
  Languages:
    Python: 2.7.16 - /usr/bin/python
  Browsers:
    Chrome: 87.0.4280.88
    Firefox: 84.0.1
    Safari: 13.0.4
  npmPackages:
    gatsby: ^2.26.1 => 2.26.1 
    gatsby-image: ^2.5.0 => 2.5.0 
    gatsby-plugin-manifest: ^2.6.1 => 2.6.1 
    gatsby-plugin-offline: ^3.4.0 => 3.4.0 
    gatsby-plugin-react-helmet: ^3.4.0 => 3.4.0 
    gatsby-plugin-sharp: ^2.8.0 => 2.8.0 
    gatsby-plugin-styled-components: ^3.7.0 => 3.7.0 
    gatsby-source-filesystem: ^2.5.0 => 2.5.0 
    gatsby-transformer-sharp: ^2.6.0 => 2.6.0 
@tmatek tmatek added the type: bug An issue or pull request relating to a bug in Gatsby label Jan 5, 2021
@gatsbot gatsbot bot added the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Jan 5, 2021
@ascorbic ascorbic added topic: styled-components and removed status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer labels Jan 14, 2021
@github-actions
Copy link

github-actions bot commented Feb 3, 2021

Hiya!

This issue has gone quiet. Spooky quiet. 👻

We get a lot of issues, so we currently close issues after 60 days of inactivity. It’s been at least 20 days since the last update here.
If we missed this issue or if you want to keep it open, please reply here.
As a friendly reminder: the best way to see this issue, or any other, fixed is to open a Pull Request. Check out gatsby.dev/contribute for more information about opening PRs, triaging issues, and contributing!

Thanks for being a part of the Gatsby community! 💪💜

@github-actions github-actions bot added the stale? Issue that may be closed soon due to the original author not responding any more. label Feb 3, 2021
@tmatek
Copy link
Author

tmatek commented Feb 3, 2021

So after further digging it turns out this is another result of hydration mismatches: #17914.

This article has helped in resolving the issue: https://www.joshwcomeau.com/react/the-perils-of-rehydration/

The solution is to check if we are in browser before applying the query param (2-pass rendering):

...

const [client, setClient] = useState(false)
useEffect(() => {
  setClient(true)
}, [])

return (
  <Button type="button" isDisabled={client && !!params.disable}>
    click me
  </Button>
)

This way the class names at compile time and during hydration in browser match.

It would be really helpful if Gatsby reported hydration mismatches during builds.

@github-actions github-actions bot removed the stale? Issue that may be closed soon due to the original author not responding any more. label Feb 4, 2021
@github-actions
Copy link

Hiya!

This issue has gone quiet. Spooky quiet. 👻

We get a lot of issues, so we currently close issues after 60 days of inactivity. It’s been at least 20 days since the last update here.
If we missed this issue or if you want to keep it open, please reply here.
As a friendly reminder: the best way to see this issue, or any other, fixed is to open a Pull Request. Check out gatsby.dev/contribute for more information about opening PRs, triaging issues, and contributing!

Thanks for being a part of the Gatsby community! 💪💜

@github-actions github-actions bot added the stale? Issue that may be closed soon due to the original author not responding any more. label Feb 24, 2021
@tmatek tmatek closed this as completed Feb 24, 2021
@LekoArts LekoArts added the topic: plugins-styling Relates to the styling (e.g. CSS-in-JS, SASS, PostCSS) label May 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale? Issue that may be closed soon due to the original author not responding any more. topic: plugins-styling Relates to the styling (e.g. CSS-in-JS, SASS, PostCSS) type: bug An issue or pull request relating to a bug in Gatsby
Projects
None yet
Development

No branches or pull requests

3 participants