|
| 1 | +# Laravel Mix Static Cache Busting |
| 2 | + |
| 3 | +<a href="https://www.npmjs.com/package/@maize-tech/laravel-mix-static-cache-busting"><img src="https://img.shields.io/npm/v/@maize-tech/laravel-mix-static-cache-busting.svg" alt="NPM"></a> |
| 4 | +<a href="https://npmcharts.com/compare/@maize-tech/laravel-mix-static-cache-busting?minimal=true"><img src="https://img.shields.io/npm/dt/@maize-tech/laravel-mix-static-cache-busting.svg" alt="NPM"></a> |
| 5 | +<a href="https://www.npmjs.com/package/@maize-tech/laravel-mix-static-cache-busting"><img src="https://img.shields.io/npm/l/@maize-tech/laravel-mix-static-cache-busting.svg" alt="NPM"></a> |
| 6 | + |
| 7 | +This package allows you to simulate Laravel `mix` function, used for cache busting, without the need to use PHP in your project. |
| 8 | + |
| 9 | +The plugin will grab your static `index.html` file, search for any asset and replace them with all occurrences specified within `mix-manifest.json`. |
| 10 | + |
| 11 | +For example, if your `index.html` file includes this stylesheet reference: |
| 12 | + |
| 13 | +```html |
| 14 | +<link rel="stylesheet" type="text/css" href="/app.css"> |
| 15 | +``` |
| 16 | + |
| 17 | +the plugin will automatically replace it during the compilation with: |
| 18 | + |
| 19 | +```html |
| 20 | +<link rel="stylesheet" type="text/css" href="/app.css?id=fbd7c81ea490324c303c"> |
| 21 | +``` |
| 22 | + |
| 23 | +### Installation |
| 24 | + |
| 25 | +You can install this package via npm: |
| 26 | + |
| 27 | +```bash |
| 28 | +npm i @maize-tech/laravel-mix-static-cache-busting --save-dev |
| 29 | +``` |
| 30 | + |
| 31 | +### Usage |
| 32 | + |
| 33 | +To use the package, all you need to do is update your `webpack.mix.js` file and add the `staticCacheBusting` method. |
| 34 | +The first parameter is the `mix-manifest.json` file path, while the second one is the `index.html` file path. |
| 35 | + |
| 36 | +Here's an example implementation of the method: |
| 37 | + |
| 38 | +```js |
| 39 | +let mix = require('laravel-mix'); |
| 40 | + |
| 41 | +require('laravel-mix-static-cache-busting'); |
| 42 | + |
| 43 | +mix |
| 44 | + // tasks that will generate a mix-manifest.json |
| 45 | + .staticCacheBusting('./dist/mix-manifest.json', './dist/index.html') // change paths at your discretion |
| 46 | +``` |
| 47 | + |
| 48 | +### Troubleshooting |
| 49 | + |
| 50 | +The plugin isn't working? |
| 51 | +Check the entry attributes in your `mix-manifest.json` and make sure your html `href` attributes follow the same path. |
| 52 | + |
| 53 | +For example, if your `mix-manifest.json` file has this attribute for the `app.css` file: |
| 54 | + |
| 55 | +```json |
| 56 | +{ |
| 57 | + "/app.css": "/app.css?id=fbd7c81ea490324c303c" |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +then your `index.html` should reference the file this way: |
| 62 | + |
| 63 | +```html |
| 64 | +<!-- note the slash before app.css --> |
| 65 | +<link rel="stylesheet" type="text/css" href="/app.css"> |
| 66 | +``` |
| 67 | + |
| 68 | +while this will **not** work: |
| 69 | + |
| 70 | +```html |
| 71 | +<!-- initial slash missing! --> |
| 72 | +<link rel="stylesheet" type="text/css" href="app.css"> |
| 73 | +``` |
| 74 | + |
| 75 | +Also, please note that this plugin will replace any entry as-is and anywhere it's placed within the targeted file (index.html). |
| 76 | + |
| 77 | +## Changelog |
| 78 | + |
| 79 | +Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. |
| 80 | + |
| 81 | +## Contributing |
| 82 | + |
| 83 | +Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details. |
| 84 | + |
| 85 | +## Security Vulnerabilities |
| 86 | + |
| 87 | +Please review [our security policy](../../security/policy) on how to report security vulnerabilities. |
| 88 | + |
| 89 | +## Credits |
| 90 | + |
| 91 | +- [Francesco Zaniol](https://github.com/francescozaniol) |
| 92 | +- [All Contributors](../../contributors) |
| 93 | + |
| 94 | +## License |
| 95 | + |
| 96 | +The MIT License (MIT). Please see [License File](LICENSE.md) for more information. |
0 commit comments