PWA를 활용하여 점검 또는 오프라인 공지를 만드는 방법입니다.
가장 빠른 점검 메시지를 적용하기 위해 .json 에 기록 후 읽어가는 방식을 활용합니다.
PWA로 개발 시 .json 파일이 cache에 물려있게 되며 이 때, 두 가지 문제가 발생합니다.
ngsw.json 에서 .json 파일 내역을 지웁니다. 즉, 캐싱되지 않도록 합니다.
최초로 load되는 module에서 json 파일을 읽기.
component는 onInit 시 해당 변수 또는 해당 subject를 subscribe하여 결과를 반영합니다.
// maintenance.json
{
"maintenance": false
}
// ngsw.json (maintenance.json 파일이 캐싱되지 않음)
{
"configVersion": 1,
"index": "/index.html",
"assetGroups": [
...
{
"name": "assets",
"installMode": "lazy",
"updateMode": "prefetch",
"urls": [
"/assets/icons/icon-128x128.png",
],
"patterns": []
}
],
"dataGroups": [],
"hashTable": {
"/assets/icons/icon-128x128.png": "dae3b6ed49bdaf4327b92531d4b5b4a5d30c7532",
...
},
"navigationUrls": [
...
]
}
// app.module.ts
export function getConfig(config: AppService) {
return async () => await config.getMaintenance();
}
...
providers: [
HttpClient,
{
provide: APP_INITIALIZER,
useFactory: getConfig,
deps: [AppService],
multi: true
}
]
// app.service.ts
getMaintenance() {
return new Promise((resolve, reject) => {
this.http.get(`${window.location.origin}/assets/maintenance.json`, { headers: new HttpHeaders({ timeout: `${3000}`})})
.toPromise().then(item => {
this.config = item['maintenance'];
resolve();
}).catch(err => {
this.offline = true;
resolve();
});
});
}
// app.component.ts
ngOnInit() {
const maintenance = this.appService.config;
const offline = this.appService.offline;
if (maintenance) {
alert('MAINTENANCE');
}
if (offline) {
alert('offline');
}
if (this.swUpdate.isEnabled) {
...
}
이제 점검 중일 때는 maintenance.json의 "maintenance" 값을 true로 수정한 뒤 이 파일만 배포하면 되며,
offline일 시 오프라인임을 알려주거나 네트워크 점검이 필요하다는 안내문구를 넣을 수 있습니다.
"maintenance"에 시간 값을 추가하여 client에서 해당 시간 범위만큼일 때만 maintenance의 값을 처리하게 할 수도 있을 것 입니다.