我正在使用Angular5制作一个白标PWA。我想知道是否可以根据URL中的信息动态更改清单文件中的png图标。我希望每个唯一组织都有不同的图标。
例如:
- www.mywebsite.com/organisation1
- www.mywebsite.com/organisation2
对于URL 1,在浏览器上安装时应该获得不同的图标,而URL 2也应如此。我不知道如何让它起作用,也不确定这是否可行。
我正在使用Angular5制作一个白标PWA。我想知道是否可以根据URL中的信息动态更改清单文件中的png图标。我希望每个唯一组织都有不同的图标。
例如:
对于URL 1,在浏览器上安装时应该获得不同的图标,而URL 2也应如此。我不知道如何让它起作用,也不确定这是否可行。
Jeff的链接会指导你正确的方向。你的问题让我很好奇,所以我写了一篇博客文章,介绍了使用Express.js实现特定实现方式。
基本上,你可以动态地服务于manifest.json文件。以下是它的要点。你可以从referrer header中获取组织名称。
manifest.hbs
{
"name": "{{orgName}} App",
"short_name": "{{orgName}}",
"icons": [{
"src": "/images/icons/{{orgName}}/app-icon-192x192.png",
"type": "image/png",
"sizes": "192x192"
}],
"start_url": "/{{orgName}}",
"display": "standalone"
}
Express路由
app.get ('/manifest.json', (req, res) => {
// You can dynamically generate your manifest here
// You can pull the data from database and send it back
// I will use a template for simplicity
//Use some logic to extract organization name from referer
var matches = /\/([a-z]+)\/?$/i.exec (req.headers.referer);
if (matches && matches.length > 1) {
var orgName = matches[1];
} else {
var orgName = 'ORGA'; //Default
}
// Need to set content type, default is text/html
res.set ('Content-Type', 'application/json');
res.render ('manifest.hbs', {orgName});
});