简体   繁体   English

self.__WB_MANIFEST 不会被 InjectManifest 生成的 url 替换,以便使用 service worker 进行预缓存

[英]self.__WB_MANIFEST is not replaced by InjectManifest generated urls for precaching using service worker

This is my webpack config这是我的 webpack 配置

    new InjectManifest({
      swSrc: './app/service-worker.js',
      swDest: 'sw.js',
      maximumFileSizeToCacheInBytes: 5000000,
    }),

This is my servie-worker.js这是我的 service-worker.js

import { skipWaiting, clientsClaim } from 'workbox-core';
import { cleanupOutdatedCaches, precacheAndRoute } from 'workbox-precaching';

skipWaiting();
clientsClaim();
cleanupOutdatedCaches();
precacheAndRoute(self.__WB_MANIFEST || []);

This is how i register serviceworker这就是我注册服务人员的方式

import { Workbox } from 'workbox-window';

const register = () => {
  // service worker should be installed only in prod env.
  if (process.env.NODE_ENV !== 'production') {
    return;
  }
  // check if browser supports SW before register.
  if ('serviceWorker' in navigator) {
    const wb = new Workbox('/company/sw.js');

    wb.register().then((registration) => {
      console.log('Registration success', registration.scope);
    }).catch((err) => {
      console.log('Registration failed', err);
    });
  }
};

register();

This is the sw.js code generated by webpack bundler using InjectManifest where it injects the route by replacing self.__WB_MANIFEST这是 webpack 捆绑器使用 InjectManifest 生成的 sw.js 代码,它通过替换 self.__WB_MANIFEST 来注入路由

    f81121bb716d06db5a3c: function (e, t, r) {
        "use strict";
        var n = r("ee2c850f71b22ec627d9"),
            a = r("71160463eb5f8808d43e");
        (0, n.skipWaiting)(), (0, n.clientsClaim)(), (0, a.cleanupOutdatedCaches)(), (0, a.precacheAndRoute)([{
            'revision': 'd6d49a7c4da3232a63313d0296cb697c',
            'url': '/company/index.html'
        },  {
            'revision': null,
            'url': '/company/static/js/main.eecf38c8e0bdeb9edfd0.chunk.js'
        }, {
            'revision': 'd77aa54cfc47caccf631a032dccdd1a4',
            'url': '/company/static/js/main.eecf38c8e0bdeb9edfd0.chunk.js.br'
        }, {
            'revision': 'f583ac2ae2e839d40f7390f44de0d09e',
            'url': '/company/static/js/main.eecf38c8e0bdeb9edfd0.chunk.js.gz'
        }, {
            'revision': null,
            'url': '/company/static/js/node_vendors.dfc2f2b312f9103e4f57.chunk.js'
        }, {
            'revision': 'cc71224b8f04e2722f7fd8e934625d99',
            'url': '/company/static/js/node_vendors.dfc2f2b312f9103e4f57.chunk.js.br'
        }, {
            'revision': 'a66582b83e005784db6aa640e3075f67',
            'url': '/company/static/js/node_vendors.dfc2f2b312f9103e4f57.chunk.js.gz'
        }, {
            'revision': null,
            'url': '/company/static/js/runtime~main.67d1bc90b93c84b2daf6.js'
        }, {
            'revision': 'e0a95983d322b621a7fd3b16888aaa8b',
            'url': '/company/sw.js.br'
        }, {
            'revision': 'e1ab2a71f411919e92a90675915af0ef',
            'url': '/company/sw.js.gz'
        }] || [])
    },

Below is the screenshot of sw.js code from devtools when served from localhost下面是从 localhost 提供时来自 devtools 的 sw.js 代码的屏幕截图在此处输入图像描述

As we could clearly see in the sw.js file served in localhost, the self.__WB_MANIFEST is not replaced by the urls generated by InjectManifest.正如我们在 localhost 中提供的 sw.js 文件中清楚地看到的那样,self.__WB_MANIFEST 没有被 InjectManifest 生成的 url 替换。 Here my question is how come the sw.js file is having different code from what its generated by bundler.在这里,我的问题是 sw.js 文件为何与捆绑程序生成的代码不同。 I tried unregister service worker and tried empty cache as well still there is difference in sw.js from what i have in build vs what served.我尝试注销服务工作者并尝试清空缓存,但 sw.js 与我在构建中所拥有的内容与所服务的内容之间仍然存在差异。 Please suggest some ideas here.请在这里提出一些想法。 Below is the version im using下面是我使用的版本

    "workbox-core": "^6.4.2",
    "workbox-precaching": "^6.4.2",
    "workbox-window": "^6.4.2",
    "workbox-webpack-plugin": "^6.4.2"

After a days effort I figured out where I went wrong.经过一天的努力,我弄清楚了哪里出错了。 Previously my webpack config was like this以前我的 webpack 配置是这样的

    new CompressionPlugin({
      filename: '[path].gz[query]',
      algorithm: 'gzip',
      test: /\.js$|\.css$|\.html$/,
      threshold: 10240,
      minRatio: 0.7,
    }),

    new BrotliPlugin({
      asset: '[path].br[query]',
      test: /\.js$|\.css$|\.html$/,
      threshold: 10240,
      minRatio: 0.7,
    }),
        new InjectManifest({
      // exclude: [/\.map$/, /asset-manifest\.json$/],
      // importWorkboxFrom: 'cdn',
      swSrc: './app/service-worker.js',
      swDest: 'sw.js',
      maximumFileSizeToCacheInBytes: 5000000,
    }),

Now I moved InjectManifest plugin before the compressions plugin which eventually solved the problem现在我将 InjectManifest 插件移到了最终解决问题的压缩插件之前

    new InjectManifest({
      // exclude: [/\.map$/, /asset-manifest\.json$/],
      // importWorkboxFrom: 'cdn',
      swSrc: './app/service-worker.js',
      swDest: 'sw.js',
      maximumFileSizeToCacheInBytes: 5000000,
    }),

    new CompressionPlugin({
      filename: '[path].gz[query]',
      algorithm: 'gzip',
      test: /\.js$|\.css$|\.html$/,
      threshold: 10240,
      minRatio: 0.7,
    }),

    new BrotliPlugin({
      asset: '[path].br[query]',
      test: /\.js$|\.css$|\.html$/,
      threshold: 10240,
      minRatio: 0.7,
    }),

Anyone has idea on how webpack handle this when making a bundle任何人都知道 webpack 在制作捆绑包时如何处理这个问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM