问题描述
我想在用户访问登录页面的清单中创建 start_url。 https://example.com/1234 https://example.com/4321
这是我的 PHP 来获取整个 url {{ Request::fullUrl() }} 我放在“start_url”:“{{ Request::fullUrl() }}”,它不会在 json 文件中获取。
我曾尝试将 manifest.json 更改为 manifest.PHP,但不知道如何将 json 内容转换为 PHP 以使其工作。
<link rel="manifest" href="/_manifest.PHP?start_url=val" data-pwa-version="set_in_manifest_and_pwa_js">
我知道这是个老问题,我在互联网上搜索仍然找不到解决方案。 Set start_url in web app manifest based on the URL that was added to homescreen 这没有解决办法。删除 start_url 不会使 PWA 工作。
有哪位专家可以帮帮我吗?欣赏! 我是编码新手,请原谅我的知识有限。
解决方法
Chrome(和大多数其他浏览器)要求您在清单中提供 start_url
,并且它不能是动态的。
您可以通过保存用户在安装 PWA 时所在的页面来构建此行为。然后,将 start_url
设置为一个简单的重定向页面,该页面会检查用户安装时所在的页面,并将其重定向到该页面。
要在安装 PWA 时保存用户所在的页面,请监听 appinstalled
事件,然后将页面保存到 localStorage:
window.addEventListener('appinstalled',() => {
localStorage['installedFrom'] = window.location.pathname;
});
然后,在重定向页面中,执行以下操作:
const installedFrom = localStorage['installedFrom'];
if (installedFrom) {
window.location.replace(installedFrom);
} else {
// No install page saved,send them to the home page?
}