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.

Hot reloading CSS broken when component lives in wrapPageElement

See original GitHub issue

Description

I’m using Styled Components (and the gatsby plugin for it) but I can confirm this is an issue with CSS modules as well.

Hot reloading of CSS works great when you are editing a component on a page.

However if you use the wrapPageElement or wrapRootElement API in gatsby-browser.js and have a styled component that lives in that layout, it’s reloads the whole page - losing at state you might have.

export function wrapPageElement({ element, props }) {
  return <Layout {...props}>{element}</Layout>;
}

So for example, if I have a Layout component:

export default function Layout({ children }) {
  return (
    <>
      <div>
          <Nav></Nav>
          {children}
      </div>
    </>
  );
}

And then either in Layout.js directry, or inside of another component - like Nav - I have my styled component:

const NavStyles = styled.nav`
  background: green;
`;

Now if I change green to blue. The whole page reloads.

If I were to move that Nav component out of Layout.js and into a page like pages/index.js, it hot reloads fine without reloading the page.

Environment

This happens in both Firefox and Chrome

System: OS: macOS 10.15.2 CPU: (16) x64 Intel® Core™ i9-9880H CPU @ 2.30GHz Shell: 3.2.57 - /bin/bash Binaries: Node: 13.9.0 - /usr/local/bin/node Yarn: 1.22.0 - /usr/local/bin/yarn npm: 6.13.7 - /usr/local/bin/npm Languages: Python: 2.7.15 - /usr/local/bin/python Browsers: Chrome: 81.0.4044.122 Firefox: 59.0.1 Safari: 13.0.4 npmGlobalPackages: gatsby: 2.15.14

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:24 (15 by maintainers)

github_iconTop GitHub Comments

4reactions
gaearoncommented, Apr 30, 2020

Is there a plan to switch to Fast Refresh by default? React Hot Loader is extremely fragile and I hope we can phase it out asap.

3reactions
blainekastencommented, Apr 30, 2020

@gaearon I’m absolutely tracking all of this 😃 Here’s our rough plan of action:

  • take part in stabilizing the refresh plugin ecosystem and keep it incorporated with gatsby
  • Gatsby V3 will raise the bar for minimum react version, which allows us to make fast-refresh the default dev experience
  • continue to support hot-loader until preact is compatible with fast-refresh

Since fast-refresh should fix these rough edges for us, I don’t think it’s worth our effort fixing this in hot-loader as it will eventually be fully replaced.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Wes Bos on Twitter: "Just logged an issue I'm having with CSS ...
Hot reloading CSS broken when component lives in wrapPageElement ... Description I'm using Styled Components (and the gatsby plugin for it) ...
Read more >
Gatsby wrapRootElement/wrapPageElement works on ...
I'm using Gatsby for the first time on a simple website project. I'm accustomed with traditional React apps where there is a root...
Read more >
Firefox Devtools: CSS state not live updating after hot reload ...
My question is: The live update of a CSS value in the devtools in Chrome works ... Not the browser window refresh, this...
Read more >
Fast Refresh | Gatsby
Fast Refresh is an implementation of Hot Reloading with full support from React. It replaces unofficial solutions like react-hot-loader .
Read more >
Gatsby Changelog | 5.3.0
Now, when a code update or data update is made for the <Header> component, only the HTML for the Slice will be updated,...
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