服务工作者 - CRA 中的 WorkBox v3.5 "Unable to find a place to inject the manifest"



我正在尝试用自定义应用程序服务工作者替换默认的 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");
}
}

我用谷歌搜索并查看其他堆栈溢出的所有内容都显示您需要包含

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。

在我的软件包中降级到 3.5.0.json 解决了这个问题。

相关内容

最新更新