0

我的问题是路径设置不正确。例如在 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();
4

1 回答 1

0

借助这个:Workbox 的预缓存清单文件在 Laravel Mix 设置中包含无效的 URL 字符串

我发现我可以这样设置我的路径:

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,
        }),
    ],
    output: {
        publicPath: '/assets/build/', // fixes the output bug
    },
});

我不确定,但在另一个问题中,他们说设置特定路径时它会失败。到目前为止,一切看起来都很好,我的 Service Worker 工作正常 :)

于 2019-08-21T16:37:47.377 回答