styled-components: Problem with hot module reloading & createGlobalStyle
System:
- OS: macOS Sierra 10.12.6
- CPU: x64 Intel® Core™ i7-4770HQ CPU @ 2.20GHz
- Memory: 105.95 MB / 16.00 GB
- Shell: 3.2.57 - /bin/bash
Binaries:
- Node: 10.1.0 - ~/.nvm/versions/node/v10.1.0/bin/node
- Yarn: 1.6.0 - /usr/local/bin/yarn
- npm: 6.1.0 - ~/.nvm/versions/node/v10.1.0/bin/npm
- Watchman: 4.7.0 - /usr/local/bin/watchman
npmPackages:
- styled-components: ^4.0.0-beta.10 => 4.0.0-beta.10
Hi there, not sure if I would call this a bug but thought it was worth pointing out.
I have started using the createGlobalStyle function in v4, but have run into some issues relating to Hot Module Reloading. Whenever a reload is triggered by something outside the createGlobalStyle function, all of its comprising css rules are lost. Here is a simple project which demonstrates this:
https://github.com/frednomoon/styled-hot-reload
It seems from this behaviour that its necessary to reload the module containing the global styles every time the page changes. But, of course, this doesn’t happen unless you actually change something inside that module.
I’m guessing its probably possible to solve this with some additional config in my bundler, but obviously would be great to avoid things like that.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 8
- Comments: 29 (8 by maintainers)
Yup, can verify that it’s not working in
v5.0.0-beta.8.FYI: while it’s fixed in
4.3.2, it seems to persist inv5.0.0-beta.8(both in conjunction with GatsbyJS).any solution guys or, maybe, workarounds? (Except manual reloading =) )
This happens in Next.js 😕 It’s pretty annoying especially since I can’t just add react-hot-loader I don’t think… /cc @timneutkens
Will reopen this, maybe there’s a better way to make
createGlobalStyleHMR-able from our side?This is still an issue for
createGlobalStyleI found that I needed to use version “styled-components”: “^5.0.1” in order for anything to work.As my collegue @gl0gl0 mention above. We where having this styles bug on localhost, using razzle. We where also having this warning on our console that said “Uncaught Error: only one instance of babel-polyfill is allowed”. So we delete the import for babel from our hydrate() file on the client side, and this change fixed the styles issue! Hopefully this can help so users with the same problem. Check that you don’t have two babel-polyfill instances being loaded.
This happens to me too with Gatsby JS with the same version as @vitalybe. Re-opening issue 😃
This still happens to me with
styled-components@4.1.3Ok figured it out. Vanilla HMR replaces the whole file, which is a problem because we keep a counter for cGS to know when to remove global styles when all instances have been removed. If you use
react-hot-loaderin conjunction with parcel’s HMR it works correctly.A unit was supplied in the tested code. The issue remains.
I think we need to figure out how to refactor away the counting mechanism. Part of the issue is the old component gets unmounted late during HMR, and the new component has already rendered, so it sees a count of one due to the HMR reset and kills the styles. On Tue, Oct 9, 2018 at 9:10 AM Max Stoiber notifications@github.com wrote:
Interesting. I’ll try and look tonight. There have been a lot of annoying edge cases around HMR lately 😕