为什么在我更改 Service Worker 后 safari 会要求允许在每个 window.open() 上弹出窗口?

问题描述 投票:0回答:1

为什么 safari 在我更改 Service Worker 后要求允许在每个 window.open() 上弹出窗口?并且不允许打开新选项卡。这是我的 Service Worker 的代码。 更新我的 Service Worker 后,我所有用户设备中安装的 PWA 应用程序现在可以在我的应用程序中打开新链接并进行导航,因为 Safari 会阻止新的点击

import { clientsClaim } from "workbox-core";
import { ExpirationPlugin } from "workbox-expiration";
import { precacheAndRoute, createHandlerBoundToURL } from "workbox-precaching";
import { registerRoute } from "workbox-routing";
import { StaleWhileRevalidate } from "workbox-strategies";

clientsClaim();

precacheAndRoute(self.__WB_MANIFEST);

const fileExtensionRegexp = new RegExp("/[^/?]+\\.[^/]+$");
registerRoute(
  // Return false to exempt requests from being fulfilled by index.html.
  ({ request, url }) => {
    // If this isn't a navigation, skip.
    if (request.mode !== "navigate") {
      return false;
    } // If this is a URL that starts with /_, skip.

    if (url.pathname.startsWith("/_")) {
      return false;
    } // If this looks like a URL for a resource, because it contains // a file extension, skip.

    if (url.pathname.startsWith("/api/")) {
      return false;
    } // If this looks like a URL for a resource, because it contains // a file extension, skip.

    if (url.pathname.match(fileExtensionRegexp)) {
      return false;
    } // Return true to signal that we want to use the handler.

    return true;
  },
  createHandlerBoundToURL(process.env.PUBLIC_URL + "/index.html")
);

// An example runtime caching route for requests that aren't handled by the
// precache, in this case same-origin .png requests like those from in public/
registerRoute(
  // Add in any other file extensions or routing criteria as needed.
  ({ url }) =>
    url.origin === self.location.origin &&
    [".png", ".webp", ".png", ".jpeg", ".jpg"].some(char => url.pathname.endsWith(char)), // Customize this strategy as needed, e.g., by changing to CacheFirst.
  new StaleWhileRevalidate({
    cacheName: "images",
    plugins: [
      new ExpirationPlugin({ maxEntries: 50 })
    ]
  })
);

self.addEventListener("message", function (event) {
  if (
    ["https://example.com", "https://preprod-example.com", "https://qc-example.com"].indexOf(
      event.origin
    ) !== -1 &&
    event.data &&
    event.data.type === "SKIP_WAITING"
  ) {
    self.skipWaiting();
  } else return;
});

self.addEventListener("fetch", function (event) {
  if (event.request.url.match("^.*(/api/).*$")) {
    return false;
  }
  if (event.request.url.indexOf("/api/") !== -1) {
    return false;
  }

  if (event.request.url.includes("/api/")) {
    return;`your text`
  }
  
  event.respondWith(fetch(event.request));
});
javascript safari service-worker workbox
1个回答
0
投票

为每个新选项卡请求弹出权限的原因与更新我的服务工作线程后异步调用 window.open() 有关,并且在异步函数之外调用它后效果很好。

© www.soinside.com 2019 - 2024. All rights reserved.