我已经使用 Angular 6 创建了一个 PWA。稍后我想要有不同的图标并启动 Urls,因为该应用程序将在多个 url 下运行(每个帐户将被分配一个唯一的 url,并且每个帐户都有不同的徽标)。所以我想动态更改 manifest.json。
有没有办法做到这一点?
编辑:
我这样尝试:
<head>
<meta charset="utf-8">
<title>Pwatest</title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="assets/icons/coffee.png">
<link rel="manifest" id="my-manifest-placeholder">
<link rel="apple-touch-icon" href="assets/icons/icon-192x192.png">
<!-- <link rel="manifest" href="manifest.json"> -->
<meta name="theme-color" content="#1976d2">
<script>
var test = true;
var myDynamicManifest = {
"name": "pwatest",
"short_name": "pwatest",
"theme_color": "#1976d2",
"background_color": "#fafafa",
"display": "standalone",
"scope": "/",
"start_url": "/",
"icons": []
}
if(test){
myDynamicManifest['icons'].push({
"src": "assets/icons/coffee-192x192.png",
"sizes": "192x192",
"type": "image/png"
});
}else{
myDynamicManifest['icons'].push({
"src": "assets/icons/frog-192x192.png",
"sizes": "192x192",
"type": "image/png"
});
}
console.log(myDynamicManifest);
const stringManifest = JSON.stringify(myDynamicManifest);
const blob = new Blob([stringManifest], {type: 'application/json'});
const manifestURL = URL.createObjectURL(blob);
document.querySelector('#my-manifest-placeholder').setAttribute('href', manifestURL);
console.log(document.querySelector('#my-manifest-placeholder'));
</script>
</head>
编辑2:
我没有找到解决方案。我现在这样尝试:
索引.html
<link rel="manifest" id="my-manifest-placeholder" href="/manifest.php">
清单.php
<?php
$test = [
"name" => "pwatest",
"gcm_user_visible_only" => true,
"short_name" => "pwatest",
"start_url" => "/",
"display" => "standalone",
"orientation" => "portrait",
"background_color" => "#fafafa",
"theme_color" => "#1976d2",
"icons" => [
"src" => "assets/icons/coffee-192x192.png",
"sizes"=> "192x192",
"type" => "image/png"
]
];
header('Content-Type: application/json');
echo json_encode($test);
?>
但我变成了完整的 php 文件而不是 JSON。
我可以这样做吗?