reactjs - Serviceworker - WorkBox v3.5 在 CRA 中“找不到注入清单的地方”
问题描述
我正在尝试用自定义替换默认的 React 应用程序服务人员,因为它将索引路由设置为“index.html”,而我需要将它转到“200.html”以进行 react-snap。
我完全按照本指南https://github.com/facebook/create-react-app/issues/5673#issuecomment-438654051
但得到错误:
UnhandledPromiseRejectionWarning: Error: Unable to find a place to inject the manifest. Please ensure that your service worker file contains the following: self.__WB_MANIFEST
at Object.injectManifest
我的 sw.js 看起来像这样 -
if ("function" === typeof importScripts) {
importScripts(
"https://storage.googleapis.com/workbox-cdn/releases/3.5.0/workbox-sw.js"
);
/* global workbox */
if (workbox) {
console.log("Workbox is loaded");
/* injection point for manifest files. */
workbox.precaching.precacheAndRoute([]);
/* custom cache rules*/
workbox.routing.registerNavigationRoute("/200.html", {
blacklist: [/^\/_/, /\/[^\/]+\.[^\/]+$/],
});
workbox.routing.registerRoute(
/\.(?:png|gif|jpg|jpeg)$/,
workbox.strategies.cacheFirst({
cacheName: "images",
plugins: [
new workbox.expiration.Plugin({
maxEntries: 60,
maxAgeSeconds: 30 * 24 * 60 * 60, // 30 Days
}),
],
})
);
} else {
console.log("Workbox could not be loaded. No Offline support");
}
}
我在其他 stackoverflow 上搜索并查看的所有内容都表明您需要包含
workbox.precaching.precacheAndRoute([]);
我是谁。所以我不知道为什么它不起作用?
我运行工作箱的反应脚本:
"scripts": {
"start": "react-scripts start",
"build": "react-scripts --max_old_space_size=4096 build && npm run build-sw",
"build-sw": "node ./src/sw-build.js",
"test": "react-scripts test",
"eject": "react-scripts eject",
"postbuild": "react-snap"
},
解决方案
问题是 workbox-build 的版本是 5.0,而代码/脚本使用的是 3.5.0。
在我的 package.json 中降级到 3.5.0 解决了这个问题。
推荐阅读
- graphql - 访问 Apollo 服务器以进行 NestJS GraphQL 测试
- floating-point - 有人能够理解这段代码中实现了什么样的除法算法吗?
- php - 使用 byethost 使用 symfony4 获取 Http“ERROR 500”
- teradata - 如何在 Teradata 中使用合并处理空时间戳
- javascript - 有什么方法可以使用 node.js 在我的 Kubernetes 应用程序中选举领导者?
- npm - 在 K6 测试中捆绑 npm 模块“cheerio”
- opencv - opencv 和 PIL 的差异影响模型预测
- java - javac中-source的使用
- jquery - 显示两个div,一个接一个,然后调用另一个页面
- javascript - 比较 JavaScript 中的两个日期(字符串)不起作用