I'm using Workbox 3.0 (webpack-plugin) and Laravel Mix (5.6) to auto-generate a ServiceWorker file.
When running the webpack compiler, the manifest file generated by Workbox for pre-cached assets looks like this:
self.__precacheManifest = [
{
"revision": "89c25ce71806a695a25e",
"url": "//js/app.js"
},
{
"revision": "89c25ce71806a695a25e",
"url": "//css/app.css"
}
];
Obviously, the URL strings are wrong and cause errors on the actual web page.
Here is my webpack.mix.js: (relevant parts)
const {InjectManifest} = require('workbox-webpack-plugin')
mix.webpackConfig({
plugins: [
new InjectManifest({
swSrc: './resources/assets/js/sw.js'
})
]
})
While InjectManifest is used here to have precaching as well as my own dynamic caching, the same happens when using the GenerateSW plugin instead.
and my source sw.js:
workbox.precaching.precacheAndRoute(self.__precacheManifest || [])
Any idea how to solve this? If I manually modify the precacheManifest, it works fine:
self.__precacheManifest = [
{
"revision": "89c25ce71806a695a25e",
"url": "./js/app.js"
},
{
"revision": "89c25ce71806a695a25e",
"url": "./css/app.css"
}
];
Steps to reproduce this:
Laravel new <proj_name>
cd <proj_name>
npm install
npm install --save-dev workbox-webpack-plugin
const { GenerateSW } = require('workbox-webpack-plugin');
mix.webpackConfig({ plugins: [new GenerateSW()] });
php artisan make:auth
and php artisan migrate
to complete the frontend scaffoldingnpm run dev
The compiled precache-manifest file looks like this:
self.__precacheManifest = [
{
"revision": "b922e094256b9404e705",
"url": "//js/app.js"
},
{
"revision": "b922e094256b9404e705",
"url": "//css/app.css"
}
];
I found the solution:
As Laravel is using the "Laravel Mix" API to configure and run WebPack, the way to configure WebPack is to modify the file webpack.mix.js
.
Jeff Posnick pointed me into the right direction. If I add the following lines into webpack.mix.js
, the compiler produces a correct precache manifest file -
const { GenerateSW } = require('workbox-webpack-plugin');
mix.webpackConfig({
plugins: [new GenerateSW()],
output: {
publicPath: ''
}
});
The solution is to provide simply an empty string for the output.publicPath configuration option of webpack.
However, if you need to provide an actual path for the publicPath option, this workaround will fail. See bug report here: https://github.com/GoogleChrome/workbox/issues/1534
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With