├── .gitignore ├── LICENSE ├── README.md ├── package.json ├── src ├── index.js ├── moduleA.js ├── moduleB.js └── vendor.js └── webpack.config.js /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Runtime data 6 | pids 7 | *.pid 8 | *.seed 9 | 10 | # Directory for instrumented libs generated by jscoverage/JSCover 11 | lib-cov 12 | 13 | # Coverage directory used by tools like istanbul 14 | coverage 15 | 16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 17 | .grunt 18 | 19 | # node-waf configuration 20 | .lock-wscript 21 | 22 | # Compiled binary addons (http://nodejs.org/api/addons.html) 23 | build/Release 24 | 25 | # Dependency directory 26 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git 27 | node_modules 28 | .idea 29 | build/ 30 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Andrey Okonetchnikov 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # webpack-long-term-cache-demo 2 | A demo webpack config showing how to enable long-term caching using Webpack. Read [Long-term caching of static assets with Webpack](https://medium.com/@okonetchnikov/long-term-caching-of-static-assets-with-webpack-1ecb139adb95#.9ro7cpngr) for details. 3 | 4 | ## To enable long-term caching of static resources produced by webpack: 5 | 6 | 1. Use `[chunkhash]` to add a content-dependent cache-buster to each file. 7 | 1. Use compiler stats to get the file names when requiring resources in HTML. 8 | 1. Generate the chunk-manifest JSON and inline it into the HTML page before loading resources. 9 | 1. Ensure that the entry point chunk containing the bootstrapping code doesn’t change its hash over time for the same set of dependencies. 10 | 1. Profit! 11 | 12 | ## How to run 13 | 14 | 1. `npm install` 15 | 2. `npm start` 16 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "webpack-long-term-cache-demo", 3 | "version": "1.0.0", 4 | "description": "A demo project which leverages long-term caching for webpack assets", 5 | "main": "", 6 | "scripts": { 7 | "start": "webpack", 8 | "test": "echo \"Error: no test specified\" && exit 1" 9 | }, 10 | "keywords": [ 11 | "webpack", 12 | "cache", 13 | "long-term", 14 | "demo", 15 | "sample" 16 | ], 17 | "author": "@okonetchnikov", 18 | "license": "ISC", 19 | "devDependencies": { 20 | "chunk-manifest-webpack-plugin": "0.0.1", 21 | "node-libs-browser": "^0.5.2", 22 | "webpack": "^1.10.1", 23 | "webpack-manifest-plugin": "^0.3.0", 24 | "webpack-md5-hash": "0.0.5" 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | var moduleA = require('./moduleA.js'); 2 | require.ensure(['./moduleB.js'], function(require) { 3 | // Lazy load 4 | var moduleB = ('./moduleB.js'); 5 | }) 6 | 7 | module.exports = [moduleA, moduleB]; 8 | -------------------------------------------------------------------------------- /src/moduleA.js: -------------------------------------------------------------------------------- 1 | module.exports = "Module A" 2 | -------------------------------------------------------------------------------- /src/moduleB.js: -------------------------------------------------------------------------------- 1 | module.exports = "Module B" 2 | -------------------------------------------------------------------------------- /src/vendor.js: -------------------------------------------------------------------------------- 1 | module.exports = "I'm a vendor module"; 2 | -------------------------------------------------------------------------------- /webpack.config.js: -------------------------------------------------------------------------------- 1 | var path = require('path'); 2 | var webpack = require('webpack'); 3 | var ManifestPlugin = require('webpack-manifest-plugin'); 4 | var ChunkManifestPlugin = require('chunk-manifest-webpack-plugin'); 5 | var WebpackMd5Hash = require('webpack-md5-hash'); 6 | 7 | module.exports = { 8 | entry: { 9 | vendor: './src/vendor.js', 10 | main: './src/index.js' 11 | }, 12 | output: { 13 | path: path.join(__dirname, 'build'), 14 | filename: '[name].[chunkhash].js', 15 | chunkFilename: '[name].[chunkhash].js' 16 | }, 17 | plugins: [ 18 | new webpack.optimize.CommonsChunkPlugin({ 19 | name: "vendor", 20 | minChunks: Infinity, 21 | }), 22 | new WebpackMd5Hash(), 23 | new ManifestPlugin(), 24 | new ChunkManifestPlugin({ 25 | filename: "chunk-manifest.json", 26 | manifestVariable: "webpackManifest" 27 | }), 28 | new webpack.optimize.OccurenceOrderPlugin() 29 | ] 30 | }; 31 | --------------------------------------------------------------------------------