PWA 清单 start_url 为 current_url

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

我有一个渐进式网络应用程序(PWA)。我有一个manifest.json。我想将清单的 start_url 设置为当前 url。安装应用程序时,有没有办法将 start_url 设置为当前 url?

注意:- 在我的 PWA 中,每个用户都会获得其帐户的唯一 URL。因此,当他们安装 PWA 时,start_url 应该是他们的 current_url(例如:https://example.com/user/id=abc1322112

我尝试使用 script.js 更改清单。没有任何效果。

javascript json url progressive-web-apps manifest
1个回答
0
投票

您无法更改清单中的 URL,但可以将其设置为 Service Worker 将重定向的虚假 URL。

清单:

{
  "start_url": "/user/me"
}

服务人员:

self.addEventListener("fetch", event => {
  const url = new URL(event.request.url);
  if (url.pathname === "/user/me") {
    const userId = /* get user id here */;
    const newUrl = `/user/id=${userId}`;
    // Redirect to the correct url
    const response = new Response(null, { status: 302, headers: { Location: newUrl } });
  } else {
    // In all other cases, just fetch from the network
    event.respondWith(fetch(event.request));
  }
});
© www.soinside.com 2019 - 2024. All rights reserved.