-
-
Notifications
You must be signed in to change notification settings - Fork 385
CSS with [contenthash] not refreshing with HMR, webpack-dev-middleware #1089
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
Comments
Please create a github repo with the reproducible example of the problem, thank you |
Thank you so much for taking the time to respond. I created a minimal example here. |
I found the root cause. It breaks when:
It works when:
It seems [contenthash] with hot module reloading does not work. I use content hash to avoid caching of the CSS file. Is there another way I can avoid caching of the css file? I updated the minimal PoC to reflect this. Here too. Attached is a screenshot of the PoC with [contenthash] and a stale CSS payload in hot reload network request. |
Weird, I can't reproduce, even more, we have test cases for this... |
Can you provide full steps using your repo above - https://github.com/YellowSaleTag/hmr-stale-css-payload/? |
Hmm interesting, sure see the following.
|
how did you resolve this problem? I found this problem in my project too |
See these comments / config
Use
as contenthash appears to break...
|
Have you tried generating your own unique hash? https://webpack.js.org/plugins/mini-css-extract-plugin/#filename-option-as-function |
thanks,It's useful for my project |
Found very easy fix - webpack/webpack@193b712#diff-9cd4ea60898beb7155d8dc56617fbb77b7688cca9c32adf59b9a656548ffd071R69, we just need to add three lines to CSS module, if someone want to fix it here - PR welcome |
As this relates to getting hot reloading with CSS files in WordPress working, I would really like to get this fixed. Since I am not really familiar with Webpack codebase, could you elaborate how this can be fixed? |
There are two issues with hot reloading CSS files in WordPress. Many CSS files are located inside the iframe, and the plugin operates only on the main document level, examples:
When I was debugging it, I also noticed that the plugin always calls mini-css-extract-plugin/src/hmr/hotModuleReplacement.js Lines 170 to 180 in 07abf0d
- if (href.indexOf(src) > -1) {
+ if (href.indexOf(url) > -1) { |
We don't support hot reloading in iframes officially. But I am fine to improve it. Can you provide reproducible test repo? |
A quick note. There was a contribution to the WordPress project that explored adding hot reloading for CSS in WordPress/gutenberg#64444. The relevant code for hot reloading support in iframes was pretty straightforward: function getAllStylesheets( win ) {
const links = [
...win.document.querySelectorAll( "link[rel='stylesheet']" ),
];
// Recursively loop through all frames with the same origin.
for ( let i = 0; i < win.frames.length; i++ ) {
try {
links.push( ...getAllStylesheets( win.frames[ i ] ) );
} catch ( err ) {
// Ignore same origin policy errors.
}
}
return links; Sure, I can think of how to set up a reproducible repo to help with that. In this particular case discussed, the challenge is that WordPress provides tools for development like npx @wordpress/create-block@latest my-block --wp-env
cd my-block
npx wp-env start
npm start -- --hot Next step is to open http://localhost:8888/wp-admin/ in the browser, log in with username: ![]() I also recorded a screencast showing how I test it in the browser. On the second screen I edited CSS in the referenced file. I had to refresh the page to see the result. wp-scripts-start-hot.movBy the way, please note that the plugin correctly detected the updated file, but the action to "Reload all css" was executed. It might be related to the iframe issue, but it could be also related to the code path I highlighted in #1089 (comment). |
By the way, please note that the plugin correctly detected the updated file, but the action to "Reload all css" was executed. It might be related to the iframe issue, but it could be also related to the code path I highlighted in #1089 (comment). I included another screencast where I disabled support for iframes to show that the CSS is correctly reloaded with HMR and the same app: wp-scripts-hot-no-iframe.movStill "Reload all css" was executed instread of targeting the single specific file I edit in the editor |
Bug report
CSS with [contenthash] not refreshing with HMR, webpack-dev-middleware
1. We see our stylesheet loaded in the document with the expected CSS.
2. We make an update to our CSS. We change the
padding
from50
to75
px.3. We see a request in the network tab for the CSS update. However, the contents is stale. It has the old value of
50
px.4. We refresh the page using the refresh button and see the updated value of
75
px.Package Versions
Webpack Config (relevant parts)
Middleware Config
Actual Behavior
padding
from50
to75
px.50
px.75
px.Expected Behavior
How Do We Reproduce?
Minimal configuration provided above for now...
Please paste the results of
npx webpack-cli info
here, and mention other relevant informationThe text was updated successfully, but these errors were encountered: