如何更改WorkboxPlugin的预缓存路径? (使用拼图Php框架)

时间:2019-08-21 07:12:36

标签: workbox workbox-webpack-plugin

我的问题是路径设置不正确。例如,在service-worker.js中,它创建:

importScripts(
  "/precache-manifest.5f7aa7298db0e8d002e09a10e9d6d1b1.js"
);

但是由于我使用的是Jigsaw Framework,并且公共位置有所不同

importScripts(
  "/assets/build/precache-manifest.5f7aa7298db0e8d002e09a10e9d6d1b1.js"
);

precache-file本身也一样,所有路径都不正确:

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();

1 个答案:

答案 0 :(得分:0)

借助以下帮助:Workbox's precache manifest file contains invalid URL strings in Laravel Mix setting

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

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
    },
});

我不确定,但是在另一个问题中,他们说设置了特定路径后它将失败。到目前为止,一切看起来都很正常,我的服务人员也可以正常工作:)