簡體   English   中英

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

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

我正在嘗試用自定義替換默認的 React 應用程序服務人員,因為它將索引路由設置為“index.html”,而我需要將 go 設置為“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 解決了這個問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM