为什么workbox webpack插件会预先添加“prepend”;“自动”;到所有资产路径?(导致应用程序启动时出现404';s)
Workbox快把我逼疯了。当我的应用程序启动时,所有预缓存的路由返回http404,因为workbox InjectManifest插件似乎在所有缓存的URL前面加上了“自动”前缀。我想知道它为什么会这样。我尝试过重新安装npm包,以匿名方式运行,清除所有缓存,等等 我使用“webpack dev server--mode development--open”运行应用程序,得到以下警告,这可能是问题的一部分:为什么workbox webpack插件会预先添加“prepend”;“自动”;到所有资产路径?(导致应用程序启动时出现404';s),webpack,workbox,workbox-webpack-plugin,Webpack,Workbox,Workbox Webpack Plugin,Workbox快把我逼疯了。当我的应用程序启动时,所有预缓存的路由返回http404,因为workbox InjectManifest插件似乎在所有缓存的URL前面加上了“自动”前缀。我想知道它为什么会这样。我尝试过重新安装npm包,以匿名方式运行,清除所有缓存,等等 我使用“webpack dev server--mode development--open”运行应用程序,得到以下警告,这可能是问题的一部分: WARNING in InjectManifest has been called
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.
我不知道为什么会出现这个错误,因为我在我的网页配置中设置了watch:false
请注意,所有路径的前面都有“auto”,返回http404。请注意,我清除了Chrome“应用程序”选项卡上的所有缓存。同样的情况也发生在匿名标签上
打开新选项卡并删除“自动”时,它工作正常:
带有注入清单的服务工作者文件显示“auto”是URL的一部分:
以前有人见过这个吗?我开始考虑从头开始写一个服务工和抛弃工作箱,但是如果我能让它正常工作,我显然会使用工作箱。
守则:
我使用workbox webpack插件在我的服务工作者中注入预缓存清单,如下所示:
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>
这里的问题是workbox webpack plugin v5与刚刚发布的webpack v5不兼容。解决方案是改用webpack v4。这里的问题是workbox webpack plugin v5与刚刚发布的webpack v5不兼容。解决方案是改用webpack v4。请参阅。这是由于
webpack
v5.x中的新行为造成的。请参见。这是由于github上的webpack
v5.x相关问题中的新行为所致:。它已在6.0.0-RC6中修复,github上的相关问题:。它固定在6.0.0-rc中
{
"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"
}
}