--- title: 'ServiceWorkerGlobalScope: install event' slug: Web/API/ServiceWorkerGlobalScope/install_event tags: - API - Reference - Service Worker - ServiceWorkerGlobalScope - events - install translation_of: Web/API/ServiceWorkerGlobalScope/install_event ---
{{DefaultAPISidebar("Service Workers API")}}
{{domxref("ServiceWorkerGlobalScope")}} インターフェイスの install イベントは、{{domxref("ServiceWorkerRegistration")}} が新しいインストール中のワーカー({{domxref("ServiceWorkerRegistration.installing")}} worker)を取得すると発生します。
| バブリング | なし |
|---|---|
| キャンセル | 不可 |
| インターフェイス | {{domxref("ExtendableEvent")}} |
| イベントハンドラープロパティ | {{domxref("ServiceWorkerGlobalScope.oninstall")}} |
次のスニペットは、install イベントハンドラーを使用してキャッシュに多数のレスポンスを設定する方法を示しています。 サービスワーカーはこれを使用して、資産をオフラインで提供できます。
this.addEventListener('install', function(event) {
event.waitUntil(
caches.open('v1').then(function(cache) {
return cache.add(
'/sw-test/',
'/sw-test/index.html',
'/sw-test/style.css',
'/sw-test/app.js',
'/sw-test/image-list.js',
'/sw-test/star-wars-logo.jpg',
'/sw-test/gallery/',
'/sw-test/gallery/bountyHunters.jpg',
'/sw-test/gallery/myLittleVader.jpg',
'/sw-test/gallery/snowTroopers.jpg'
);
})
);
});
次のように {{domxref("ServiceWorkerGlobalScope.oninstall")}} プロパティを使用してイベントハンドラーを設定することもできます。
globalScope.oninstall = function(event) {
...
};
| 仕様 | 状態 | コメント |
|---|---|---|
| {{SpecName('Service Workers', '#service-worker-global-scope-install-event', 'install')}} | {{Spec2('Service Workers')}} |
{{Compat("api.ServiceWorkerGlobalScope.install_event")}}
activate イベント