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.

When using favicon + output.clean + webpack-dev-server, the second compile loses the favicon

See original GitHub issue

Current behaviour πŸ’£

When using a favicon, `output.clean = true, and webpack-dev-server, the favicon is available after startup compile, but after the second compile it is no longer served (404 error).

Expected behaviour β˜€οΈ

My expectation is that the favicon should still be available on subsequent compiles.

Reproduction Example πŸ‘Ύ

I have a full example here: https://github.com/hiddenwaffle/favicon-clean . To run: npx webpack serve. The main.js and favicon.ico files are trivial. Below is the webpack config:

const HtmlWebpackPlugin = require('html-webpack-plugin')

module.exports = {
  mode: 'development',
  devServer: {
    contentBase: false
  },
  entry: './main.js',
  output: {
    clean: true
  },
  plugins: [
    new HtmlWebpackPlugin({
      favicon: 'favicon.ico'
    })
  ]
}

As a workaround, I can comment out contentBase: false which allows webpack-dev-server to serve the favicon from the file system. Another workaround is that I can move the clean to a production-only config.

I tried looking around the html-webpack-plugin to see if I could find anything. I noticed that if I commented out these lines in the html-webpack-plugin index.js file, things seemed to work (obviously not a good fix but hopefully it helps with understanding the nature of the issue):

          // If the template and the assets did not change we don't have to emit the html
          const newAssetJson = JSON.stringify(getAssetFiles(assets));
          // if (isCompilationCached && options.cache && assetJson === newAssetJson) {
          //   previousEmittedAssets.forEach(({ name, html }) => {
          //     compilation.emitAsset(name, new webpack.sources.RawSource(html, false));
          //   });
          //   return callback();
          // } else {
            previousEmittedAssets = [];
            assetJson = newAssetJson;
          // }

I also saw that the function that emits the favicon run only once (on startup), and that makes me think that the reason it works when cleaning is off is because the webserver still has a copy. Perhaps the favicon should be emitted as many times as the index.html is?

Environment πŸ–₯

node -e "var os=require('os');console.log('Node.js ' + process.version + '\n' + os.platform() + ' ' + os.release())"
npm --version
npm ls webpack
npm ls html-webpack-plugin
Node.js v14.15.4
darwin 19.6.0
7.5.2
favicon@ /Users/jimmy/dev/study/webpack/favicon
β”œβ”€β”¬ html-webpack-plugin@5.3.1
β”‚ └── webpack@5.35.0 deduped
β”œβ”€β”¬ webpack-cli@4.6.0
β”‚ β”œβ”€β”¬ @webpack-cli/configtest@1.0.2
β”‚ β”‚ └── webpack@5.35.0 deduped
β”‚ └── webpack@5.35.0 deduped
β”œβ”€β”¬ webpack-dev-server@3.11.2
β”‚ β”œβ”€β”¬ webpack-dev-middleware@3.7.3
β”‚ β”‚ └── webpack@5.35.0 deduped
β”‚ └── webpack@5.35.0 deduped
└─┬ webpack@5.35.0
  └─┬ terser-webpack-plugin@5.1.1
    └── webpack@5.35.0 deduped

favicon@ /Users/jimmy/dev/study/webpack/favicon
└── html-webpack-plugin@5.3.1

Please let me know if there is more I can do to help.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:6
  • Comments:6

github_iconTop GitHub Comments

1reaction
sgeracecommented, Aug 12, 2022

Any update on this issue? Seems like it might be related to #1709.

0reactions
Bessonovcommented, Apr 16, 2022

activity

Read more comments on GitHub >

github_iconTop Results From Across the Web

Webpack Dev Server - favicon not showing on localhost but ...
Favicon caching is VERY nasty. I suggest you to test your favicon with a browser you normally don't use (so it didn't cache...
Read more >
An Annotated webpack 4 Config for Frontend Web…
Any time a process becomes complicated, we break it down into manageable components, and automate the build process with tooling.
Read more >
How to Add a Favicon Icon to Your WordPress Site - WP Engine
In this article, we'll cover everything you need to know about favicons: what they are, how they'll help you, best practices for using...
Read more >
CopyWebpackPlugin | webpack
Copies individual files or entire directories, which already exist, to the build directory. Getting Started. To begin, you'll need to install copy-webpack-Β ...
Read more >
Adding favicons in a multi-browser multi-platform world
The ICO format is an image format for displaying icons, and has its roots in the first version of Windows operating systemβ€”Microsoft Windows...
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