Workbox 让我发疯。当我的应用程序启动时,所有预缓存的路由都返回 http404,因为工作箱 InjectManifest 插件似乎将“自动”添加到所有缓存的 url。我必须知道它为什么这样做。我尝试过重新安装 npm 包、以隐身模式运行、清除所有缓存等。
我使用“webpack-dev-server --mode development --open”运行应用程序,我收到以下警告,这可能是问题的一部分:
WARNING in InjectManifest has been called multiple times, perhaps due to running webpack in --watch mode. The precache manifest generated after the first call may be inaccurate! Please see https://github.com/GoogleChrome/workbox/issues/1790 for more information.
我不知道为什么会出现这个错误,因为我在 webpack 配置中设置了 watch:false。
请注意所有路径如何在前面有“auto”并返回 http404。请注意,我清除了 Chrome 的“应用程序”选项卡上的所有缓存。同样的情况也发生在隐身标签中。
当打开一个新标签并删除“自动”时,它工作正常:
带有注入清单的 service worker 文件显示“auto”是 url 的一部分:
有没有人见过这个?我开始考虑从头开始编写服务工作者并放弃工作箱,但如果我能让工作箱正常工作,我显然更愿意使用工作箱。
编码:
我使用 workbox-webpack-plugin 在我的 service worker 中注入预缓存清单,如下所示:
import {precacheAndRoute} from 'workbox-precaching';
import {registerRoute} from 'workbox-routing';
import {CacheFirst} from 'workbox-strategies';
// Use the imported Workbox libraries to implement caching,
// routing, and other logic:
precacheAndRoute(self.__WB_MANIFEST || []);
registerRoute(
({request}) => request.destination === 'image',
new CacheFirst({cacheName: 'images'}),
);
我的 webpack.config.js 如下所示,没什么特别的:
const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const {InjectManifest} = require('workbox-webpack-plugin');
module.exports = {
watch: false,
entry: path.resolve(__dirname, './src/index.js'),
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].[contenthash].js',
},
module: {
rules: [{test: /\.js$/, exclude: /node_modules/, loader: 'babel-loader'}, {
test: /\.(png|svg|jpg|gif)$/,
use: [
'file-loader',
],
}]
},
plugins: [
new HtmlWebpackPlugin({
title: 'Prototype webpack + react + workbox usage',
template: './src/index.html',
filename: './index.html',
'meta': {
'viewport': 'width=device-width, initial-scale=1.0',
'charset': 'UTF-8'
}
}),
new InjectManifest({
swSrc: './service-worker.js',
swDest: './workbox-sw-generated.js',
})
]
};
我的 index.html 也很简单:
<!DOCTYPE html>
<html lang="en">
<head>
<title><%= htmlWebpackPlugin.options.title %></title>
<script>
if ('serviceWorker' in navigator) {
window.addEventListener('load', () => {
navigator.serviceWorker.register('/workbox-sw-generated.js')
});
}
</script>
</head>
<body>
<section id="index"></section>
</body>
</html>
这是我的 package.json:
{
"name": "simple_webpack_boilerplate",
"version": "1.0.0",
"description": "A ready to use simple webpack boilerplate for react",
"main": "src/index.js",
"scripts": {
"start": "webpack-dev-server --mode development --open",
"build": "webpack --mode production"
},
"author": "Willem",
"license": "ISC",
"devDependencies": {
"@babel/core": "7.11.4",
"@babel/preset-env": "7.11.0",
"@babel/preset-react": "7.10.4",
"babel-loader": "8.1.0",
"file-loader": "^6.1.1",
"html-webpack-plugin": "4.4.1",
"terser-webpack-plugin": "^4.1.0",
"webpack": "^5.0.0",
"webpack-cli": "^3.3.12",
"webpack-dev-server": "3.11.0",
"workbox-webpack-plugin": "^5.1.4"
},
"dependencies": {
"lodash": "^4.17.20",
"react": "16.13.1",
"react-dom": "16.13.1",
"react-router-dom": "^5.2.0"
}
}