Progressive web apps を実装した
<link rel="manifest" href="path/to/app.webmanifest">
<script>navigator.serviceWorker?.register("/service-worker.js").then(_onFulfilled).catch(_onRejected)</script>
Service Worker
// service-worker.js
self.addEventListener("install", _oninstall);
self.addEventListener("activate", _onactivate);
self.addEventListener("fetch", _onfetch);
installイベント
installイベントself.addEventListener("install", event => event.waitUntil(Promise.all([_addResourcesToCache(), self.skipWaiting()])));
ServiceWorkerGlobalScope.skipWaiting
ServiceWorkerGlobalScope.skipWaitingactivateイベント
activateイベントself.addEventListener("activate", event => event.waitUntil(Promise.all([
!self.registration.navigationPreload ? Promise.resolve() : self.registration.navigationPreload.enable(),
_deleteOldCaches(),
self.clients.claim(),
])));
NavigationPreloadManager.enable
NavigationPreloadManager.enableClients.claim
Clients.claimfetchイベント
fetchイベントself.addEventListener("fetch", event => _cacheFirst());
キャッシュ
<ifModule mod_headers.c>
Header set Cache-Control "max-age=0"
</ifModule>
max-age
max-ageWeb app manifests
{
"short_name": "アプリの名前",
"name": "アプリの名前",
"icons": [{
"src": "path/to/icon.svg",
"type": "image/svg+xml",
"purpose": "monochrome",
"sizes": "any"
}],
"description": "説明"
}
必須
short_name又はname
short_name又はnameicons
iconspurposeでアイコンの用途が指定できます。monochrome、maskable、anyなどです。
start_url
start_url"start_url": "/"
background_color
background_color"background_color": "black"
background-colorと一致しているとよいようです。
display
display"display": "standalone"
display_overrideでその順番が変更できます。
scope
scope"scope": "/"
theme_color
theme_color"theme_color": "yellow"
<meta name="theme-color">タグでも指定できます。media属性と合わせてシステムのテーマに対応することもできます。
<meta name="theme-color" media="(prefers-color-scheme: light)" content="yellow">
<meta name="theme-color" media="(prefers-color-scheme: dark)" content="lightyellow">
yellow、ダークモードのときにlightyellowをテーマカラーとするよう指定しています。
lang
lang"lang": "ja-JP"
orientation
orientation"orientation": "any"