简体   繁体   English

Serviceworker - WorkBox v3.5 在 CRA 中“找不到注入清单的地方”

[英]Serviceworker - WorkBox v3.5 “Unable to find a place to inject the manifest” in CRA

I'm trying to replace the default React app serviceworker with a custom one because its setting the index route to "index.html" whereas I need to it go to "200.html" for react-snap.我正在尝试用自定义替换默认的 React 应用程序服务人员,因为它将索引路由设置为“index.html”,而我需要将 go 设置为“200.html”以进行 react-snap。

I'm following this guide EXACTLY https://github.com/facebook/create-react-app/issues/5673#issuecomment-438654051我完全按照本指南https://github.com/facebook/create-react-app/issues/5673#issuecomment-438654051

But get the error:但得到错误:

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

My sw.js looks like this -我的 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");
  }
}

Everything I've googled and looked on other stackoverflows shows you need to include我在其他 stackoverflow 上搜索并查看的所有内容都表明您需要包含

  workbox.precaching.precacheAndRoute([]);

Which I am.我是谁。 So I don't know why its not working?所以我不知道为什么它不起作用?

My react-scripts to run the workbox:我运行工作箱的反应脚本:

 "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"
  },

The problem was that the version of workbox-build was 5.0 whereas the code/script was using 3.5.0.问题是 workbox-build 的版本是 5.0,而代码/脚本使用的是 3.5.0。

Downgrading to 3.5.0 in my package.json fixed the issue.在我的 package.json 中降级到 3.5.0 解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM