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

CSS Modules (Gatsby v2) - New css styles not hot reloading. Requires a page refresh #10316

Closed
jonlow opened this issue Dec 5, 2018 · 7 comments

Comments

@jonlow
Copy link
Contributor

jonlow commented Dec 5, 2018

Description

When adding a new class to a css module, hot reload does not inject the css module class and no styles are shown. Manually refreshing the page will show the new css module class and the correct styles.

Once the page has been manually refreshed after creating a new class, any subsequent changes to the class will hot reload correctly.

Screencast of the issue https://www.useloom.com/share/a1061fb2d4344942af1f9efa4870d965

Note: I've checked gatsby v1 and new css modules display correctly without a page refresh

Steps to reproduce

I can confirm this is an issue using the latest:

Expected result

New css module classes should hot reload and display on the page (after saving) without a manual page refresh

Actual result

No styles show up on the page. A manual page refresh is required

Environment

System:
OS: macOS High Sierra 10.13.6
CPU: (4) x64 Intel(R) Core(TM) i7-4650U CPU @ 1.70GHz
Shell: 3.2.57 - /bin/bash
Binaries:
Node: 8.11.3 - /usr/local/bin/node
Yarn: 1.12.3 - ~/.yarn/bin/yarn
npm: 4.2.0 - ~/.npm-packages/bin/npm
Browsers:
Chrome: 70.0.3538.110
Firefox: 63.0.3
Safari: 12.0.1
npmPackages:
gatsby: ^2.0.53 => 2.0.63
gatsby-image: ^2.0.20 => 2.0.22
gatsby-plugin-manifest: ^2.0.9 => 2.0.11
gatsby-plugin-offline: ^2.0.16 => 2.0.18
gatsby-plugin-react-helmet: ^3.0.2 => 3.0.4
gatsby-plugin-sharp: ^2.0.14 => 2.0.14
gatsby-source-filesystem: ^2.0.8 => 2.0.10
gatsby-transformer-sharp: ^2.1.8 => 2.1.9
npmGlobalPackages:
gatsby-cli: 2.4.6

@jonlow jonlow changed the title CSS Modules (Gatsby v2) - New styles not hot reloading. Requires a page refresh CSS Modules (Gatsby v2) - New css styles not hot reloading. Requires a page refresh Dec 5, 2018
@DSchau
Copy link
Contributor

DSchau commented Dec 6, 2018

If you check your console when this happens, do you see something like the below?

Error: Aborting CSS HMR due to changed css-modules locals.

It seems to possibly be related to webpack-contrib/style-loader#320

@jonlow
Copy link
Contributor Author

jonlow commented Dec 7, 2018

@DSchau sure does!. Happens when you create a new class in a component css file and save. Once you do a manual refresh the error doesn't happen.

image

@jonlow
Copy link
Contributor Author

jonlow commented Dec 10, 2018

I think it's related to changes introduced in this PR webpack-contrib/style-loader#298

@wearegoose
Copy link

I'm facing the same issue. Does anybody know how to resolve the issue?

@jonlow
Copy link
Contributor Author

jonlow commented Jan 7, 2019

@wearegoose

As per recent convos in webpack-contrib/style-loader#320, it seems that a full page reload is aways required for CSS Modules when using react hot loader like Gatsby does. I've managed to fix it using the below...

The solution is to pass {hmr: false} to style-loader when CSS modules are being used.

This will mean passing the hmr option to miniCssExtract in webpack-utils.js (eg miniCssExtract({hmr: false})) in gatsby core when CSS modules are being used. @DSchau is there an elegant way to do this? Otherwise I'll make a PR.

In my case, I'm using css modules with the SASS plugin, so it would make sense to make a PR and add the hmr: false to the plugin itself like so:
image

@jonlow
Copy link
Contributor Author

jonlow commented Jan 23, 2019

Can I get someone to please review my PR? #11032

This is for SASS files, but I'll add another PR for regulare CSS modules once the SASS PR gets through

@wardpeet
Copy link
Contributor

PR is merged! thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants