Er *_*wal 4 progressive-web-apps manifest.json
我在 manifest.json 中使用静态变量创建了一个功能,例如:
- 姓名 - 简称 -- start_url
它适用于“添加到主屏幕”。当我动态设置 manifest.json 变量时,添加到屏幕不起作用
基本上我有一个电子商务 PWA。我的要求如下:如果用户访问应用程序 url(例如: www.example.com/products/PRODUCT_NAME_A ),他应该能够根据不同的 URL 在主屏幕或他们想要的许多上创建快捷链接。
您还可以检查我迄今为止在 javascript 中所做的代码:
var domain = document.location.origin;
var currentUrl = window. location. href;
var myDynamicManifest = {
"name": "My App",
"short_name": "My App",
"description": "This is my App",
"start_url": currentUrl,
"scope": ".",
"background_color": "#5F6EDD",
"theme_color": "#efbc4b",
"orientation": "portrait",
"display": "standalone",
"lang": "en",
"dir": "ltr",
"icons": [
{
"src" : domain + "/images/logo/logo_70x70.png",
"sizes" : "70x70",
"type" : "image/png"
},
{
"src" : domain + "/images/logo/logo_120x120.png",
"sizes" : "120x120",
"type" : "image/png"
},
{
"src" : domain + "/images/logo/logo_144x144.png",
"sizes" : "144x144",
"type" : "image/png"
},
]
}
const stringManifest = JSON.stringify(myDynamicManifest);
const blob = new Blob([stringManifest], {type: 'application/javascript'});
const manifestURL = URL.createObjectURL(blob);
document.querySelector('#manifest').setAttribute('href', manifestURL);
Run Code Online (Sandbox Code Playgroud)
我预计输出是这样的:http : //prntscr.com/oej48u
小智 9
我意识到这是一个老问题。但是,我想我会传递我是如何解决这个问题的。我的前端通过 Netlify 提供服务,后端 API 都是 GCP Cloud Functions。我添加了一个 Cloud Functions 函数来响应/manifest?startUrl=...&name=...并返回正确的 manifest.json 内容。然后,在单页应用程序中,它会<link />在页面加载或用户导航时动态更新标题以指向适当的 URL。
客户端:
const manifest = document.getElementById('manifest');
const nextManifest = document.createElement('link');
nextManifest.id = 'manifest';
nextManifest.rel = 'manifest';
nextManifest.href =
`https://<cloud function host>/manifest?startUrl=${
encodeURIComponent(`/events/${token}`)
}&name=${encodeURIComponent(event.name)}`;
manifest.parentNode.replaceChild(nextManifest, manifest);
Run Code Online (Sandbox Code Playgroud)
服务器端:
const manifest = {
"short_name": "Photo Feed",
"name": "Photo Feed",
"icons": [
{
"src": "shortcut-icon.png",
"sizes": "16x16",
"type": "image/png"
},
{
"src": "mobile-app-icon.png",
"sizes": "180x180",
"type": "image/png"
}
],
"start_url": ".",
"display": "standalone",
"theme_color": "#591A0C",
"background_color": "#06B8B3"
};
/**
* Responds to any HTTP request.
*
* @param {!express:Request} req HTTP request context.
* @param {!express:Response} res HTTP response context.
*/
exports.manifest = (req, res) => {
res.set('Access-Control-Allow-Origin', '*');
if (req.method === 'OPTIONS') {
res.set('Access-Control-Allow-Methods', 'GET');
res.set('Access-Control-Allow-Headers', 'Content-Type');
res.set('Access-Control-Max-Age', '3600');
res.status(204).send('');
return;
}
if (req.method === 'GET') {
// take /?startUrl=<url> and return a manifest file
// with `star_url`, same with name
if (req.query.startUrl) {
res.json({
...manifest,
short_name: req.query.name || manifest.short_name,
name: req.query.name ? `${req.query.name} Photo Feed` : manifest.name,
start_url: req.query.startUrl,
});
} else {
res.json(manifest);
}
}
res.status(405).send();
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5740 次 |
| 最近记录: |