Published on

Understanding Output Hashing in Webpack

Authors
  • avatar
    Name
    Hieu Cao
    Twitter

What is Output Hashing?

Output hashing is a feature in Webpack that appends a unique hash to the names of your output files. This helps browsers identify when files have changed, ensuring users always receive the most up-to-date assets while benefiting from caching for unchanged files.

For example, instead of generating bundle.js, Webpack can produce a file like bundle.abc123.js. This hash changes whenever the content of the file changes.


Why Use Output Hashing?

1. Efficient Caching

Browsers cache static assets like JavaScript and CSS files to reduce load times. With output hashing, files only get new names when their content changes, preventing stale files from being served.

2. Cache Invalidation

When you update your application, users automatically receive the latest files because the hash changes, and the browser treats it as a new file.


Implementing Output Hashing in Webpack

Step 1: Modify the output Configuration

In your webpack.config.js, update the output.filename option to include a hash:

const path = require('path')

module.exports = {
  entry: './src/index.js',
  output: {
    filename: 'bundle.[contenthash].js', // Add content hash to the file name
    path: path.resolve(__dirname, 'dist'),
    clean: true, // Clean the output directory before each build
  },
  mode: 'development',
}

Step 2: Use [contenthash] for Long-Term Caching

  • [hash]: A generic hash based on the entire build.
  • [chunkhash]: A hash based on the chunk content.
  • [contenthash]: A hash based on the content of the specific file (recommended for better caching).

In most cases, [contenthash] is preferred as it provides the best caching behavior.

Step 3: Here is my full configuration

const path = require('path')

module.exports = {
  entry: './src/index.js',
  output: {
    filename: 'bundle.[contenthash].js', // Add content hash to the file name
    path: path.resolve(__dirname, 'dist'),
    clean: true, // Clean the output directory before each build
  },
  mode: 'development',
  devServer: {
    static: {
      directory: path.join(__dirname, 'dist'),
    },
    compress: true, // Enable gzip compression
    port: 9000, // Specify the port
    open: true, // Automatically open the browser
    hot: true, // Enable Hot Module Replacement (HMR)
  },
}

Testing Output Hashing

  1. Build your project:
npm run build

or

npx webpack
  1. Check the dist directory for hashed output files, such as:
  • bundle.abc123.js
  • bundle.abc123.css

Best Practices for Output Hashing

  1. Set clean: true in the output configuration: Ensures the output directory is cleaned before each build to remove old files.

  2. Separate runtime and vendor code: Use plugins like SplitChunksPlugin to split vendor and runtime code into separate files, enhancing caching efficiency.


Conclusion

Output hashing is a powerful feature in Webpack that ensures efficient caching and automatic cache invalidation. By appending unique hashes to your file names, you can significantly enhance the performance and reliability of your applications. Start leveraging output hashing today for a smoother user experience!