这是我的webpack配置
new InjectManifest({
swSrc: './app/service-worker.js',
swDest: 'sw.js',
maximumFileSizeToCacheInBytes: 5000000,
}),
这是我的servie-worker.js
import { skipWaiting, clientsClaim } from 'workbox-core';
import { cleanupOutdatedCaches, precacheAndRoute } from 'workbox-precaching';
skipWaiting();
clientsClaim();
cleanupOutdatedCaches();
precacheAndRoute(self.__WB_MANIFEST || []);
我就是这样注册服务生的
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();
这是webpack使用InjectManifest生成的InjectManifest代码,它通过替换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'
}] || [])
},
下面是从本地主机提供devtools的sw.js代码的屏幕截图
正如我们在本地主机中所提供的sw.js文件中清楚地看到的那样,self.__WB_MANIFEST没有被InjectManifest生成的urls所取代。在这里,我的问题是,为什么sw.js文件的代码与邦德勒生成的代码不同。我尝试过取消注册服务工作者,也尝试了空缓存,但是sw.js与我在构建和服务方面的不同。请在这里提出一些建议。下面是im使用的版本
"workbox-core": "^6.4.2",
"workbox-precaching": "^6.4.2",
"workbox-window": "^6.4.2",
"workbox-webpack-plugin": "^6.4.2"
发布于 2022-01-21 07:18:35
经过一天的努力,我知道自己哪里出了问题。以前我的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,
}),
现在,我在压缩插件之前移动了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,
}),
谁知道webpack在做包袱时是如何处理这个问题的?
https://stackoverflow.com/questions/70781427
复制相似问题