Cache breaks asset module live reloading
See original GitHub issueBug report
What is the current behavior?
Live reloading breaks for assets using asset/resource
rule type when caching is enabled.
If the current behavior is a bug, please provide the steps to reproduce.
Add an asset loader rule:
{
test: /\.(png|svg|jpg|jpeg|gif)$/i,
type: "asset/resource",
},
Import an asset
import React from "react";
import ReactDOM from "react-dom";
import logoSrc from "./assets/logo.png";
import "./styles.less";
const App = (): JSX.Element => (
<div>
<img src={logoSrc} alt="logo" />
<div>Hello, React!</div>
</div>
);
ReactDOM.render(<App />, document.getElementById("root"));
The initial page load works as expected.
Commenting out the img
and/or import, then reverting causes the image asset to be not found.
$ curl http://localhost:9009/assets/logo-ff7cf555f3ab2107e716.png
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot GET /assets/logo-120x120@2x-ff7cf555f3ab2107e716.png</pre>
</body>
</html>
Disabling the cache “fixes” the issue
// webpack.config.js
module.exports = {
...
cache: false,
...
}
It’s also not an issue when using asset/inline
.
What is the expected behavior? I know “it should work” is not helpful, but it seems self explanatory here.
Other relevant information: webpack version: 5.36.1 Node.js version: 10.24.1 Operating System: OS X 10.14.6 Additional tools:
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:22 (17 by maintainers)
Top Results From Across the Web
Asset Versioning & Cache Busting > Webpack Encore
This is a built-in feature that tells Symfony to look for a JSON file at this path, and to use it to lookup...
Read more >Caching - webpack
When the asset's content changes, [contenthash] will change as well. Let's get our project set up using the example from getting started with...
Read more >Webpack-dev-server compiles files but does not refresh or ...
So with this setup, I run npm run dev . The webpack-dev-server starts up, the module loader test works (i.e. when I save...
Read more >Is there a way to force a browser cache refresh? - ServiceNow
Some of my client/UI scripts are stale whenever we deploy to production, breaking some of our data-filling automation.
Read more >Bust That Cache Through A Content Hash - Alain Schlesser
You want to have your static assets be cached by the visitor's ... that changes the filename with a live-reloading mechanism that uses ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
thanks alex - sorry I had a long work day yesterday… let me take a look 😃
to summarise the issue to ensure we are on the same page:
index.html
is depending on'./images/photo.jpg
clean
deletes all assets.html
assetsCan we move this bug to the html-webpack-plugin issue tracker?
Maybe a short note on
cache:true/false
- the cache option was introduced long long time ago…back then webpack would emit files again automatically.
basically it does the folloing
cache:false
will execute the template (think ofthis.importModule
) for every compilation to allow accessing the latest compilation objectcache:true
will compile and execute only if the html entry file or one of its dependencies changeCaching is a tricky topic and 7 years ago when the html-webpack-plugin was created it was very difficult to have a good performance for child compiler especially without an entry point in webpack
I am happy for better caching ideas - or even better remove them if they are no longer needed…
Could you please try to explain what you mean by “use loader for cache you need just static function by developer”?
Do you mean the
templateContent
feature?one challenge for caching was that some people use the HtmlWebpackPlugin to generate many many many files out of the same template e.g.:
Do you think we could solve all those cases with an additional
Module
? I am very happy if we can find a better caching logic… It adds a lot of complexity to the html-webpack-pluginhttps://github.com/alexilin83/webpack_test_image