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.

Cache breaks asset module live reloading

See original GitHub issue

Bug 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.

webpack.config.js

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:open
  • Created 2 years ago
  • Reactions:1
  • Comments:22 (17 by maintainers)

github_iconTop GitHub Comments

1reaction
jantimoncommented, Oct 7, 2021

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
  • ✅ HtmlWebpackPlugin is emitting everything correctly
  • ✅ the user triggers a new compile
  • ✅ webpack clean deletes all assets
  • 🕷 HtmlWebpackPlugin is emitting only .html assets
  • 💣 the image is missing

Can 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 of this.importModule) for every compilation to allow accessing the latest compilation object
  • cache:true will compile and execute only if the html entry file or one of its dependencies change

Caching 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?

new HtmlWebpackPlugin({
  templateContent: ({htmlWebpackPlugin}) => `
    <html>
      <head>
        <title>Hello World</title>
      </head>
      <body>
        <h1>Hello World</h1>
      </body>
    </html>
  `
})

one challenge for caching was that some people use the HtmlWebpackPlugin to generate many many many files out of the same template e.g.:

new HtmlWebpackPlugin({
  filename: "[name].html"
})

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-plugin

1reaction
alexilin83commented, Oct 6, 2021

Please provide reproducible test repo

https://github.com/alexilin83/webpack_test_image

Read more comments on GitHub >

github_iconTop 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 >

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