我的问题是路径设置不正确。例如在 service-worker.js 它创建:
importScripts(
"/precache-manifest.5f7aa7298db0e8d002e09a10e9d6d1b1.js"
);
但是由于我使用的是 Jigsaw Framework 并且公共位置不同,所以它必须是
importScripts(
"/assets/build/precache-manifest.5f7aa7298db0e8d002e09a10e9d6d1b1.js"
);
预缓存文件本身也是如此,所有路径都是错误的:
self.__precacheManifest = (self.__precacheManifest || []).concat([
{
"revision": "43301cc5590faa3a374f",
"url": "//js/main.js"
},
{
"revision": "43301cc5590faa3a374f",
"url": "/css/main.css"
},
一定是
self.__precacheManifest = (self.__precacheManifest || []).concat([
{
"revision": "43301cc5590faa3a374f",
"url": "/assets/build/js/main.js"
},
{
"revision": "43301cc5590faa3a374f",
"url": "/assets/build/css/main.css"
},
我尝试使用 globDirectory 设置,但它不起作用。
这是我的“标准”Webpack 配置:
const mix = require('laravel-mix');
const WorkboxPlugin = require('workbox-webpack-plugin');
const tailwindcss = require('tailwindcss');
const build = require('./tasks/build.js');
mix.disableSuccessNotifications();
mix.setPublicPath('source/assets/build');
mix.webpackConfig({
plugins: [
build.jigsaw,
build.browserSync(),
build.watch(['source/**/*.md', 'source/**/*.php', 'source/**/*.scss', '!source/**/_tmp/*']),
new WorkboxPlugin.GenerateSW({
// these options encourage the ServiceWorkers to get in there fast
// and not allow any straggling "old" SWs to hang around
clientsClaim: true,
skipWaiting: true,
}),
],
});
mix.js('source/_assets/js/main.js', 'js')
.sass('source/_assets/sass/main.scss', 'css')
.options({
processCssUrls: false,
postCss: [tailwindcss()],
})
.version();