Answer the question
In order to leave comments, you need to log in
How to include Service Worker along with webpack.mix version control in laravel?
There is a SPA application on laravel + vue.js. Using webpack mix, the application is assembled into the app.js file, and then the hash for the file is calculated using mix.version().
<!doctype html>
<html lang="ru">
<head>
...
<link href="{{ mix('/css/app.css') }}" rel="stylesheet">
...
</head>
<body id="body">
<div id="app">
</div>
<script>
if ('serviceWorker' in navigator) {
window.addEventListener('load', function () {
navigator.serviceWorker.register('/service-worker.js').then(function (registration) {
console.log('ServiceWorker registration successful with scope: ', registration.scope);
}, function (err) {
console.log('ServiceWorker registration failed: ', err);
});
});
}
</script>
<script src="{{ mix('/js/app.js') }}"></script> //Тут подключается файл
</body>
</html>
mix.webpackConfig({
plugins: [
new BundleAnalyzerPlugin(),
new SWPrecacheWebpackPlugin({
cacheId: 'pwa',
filename: 'service-worker.js',
staticFileGlobs: ['public/**/*.{css,eot,svg,ttf,woff,woff2,js,html,mp3}'],
minify: true,
stripPrefix: 'public/',
handleFetch: true,
dynamicUrlToDependencies: {
'/': ['resources/views/spa.blade.php'],
},
staticFileGlobsIgnorePatterns: [/\.map$/, /mix-manifest\.json$/, /manifest\.json$/, /service-worker\.js$/],
navigateFallback: '/',
runtimeCaching: [
{
urlPattern: /^https:\/\/fonts\.googleapis\.com\//,
handler: 'cacheFirst'
},
{
urlPattern: /^https:\/\/www\.thecocktaildb\.com\/images\/media\/drink\/(\w+)\.jpg/,
handler: 'cacheFirst'
}
],
})
],
output: {publicPath: "/", chunkFilename: "js/[name]-[hash].js"}
});
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question