CacheStorage: has() メソッド

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since April 2018.

安全なコンテキスト用: この機能は一部またはすべての対応しているブラウザーにおいて、安全なコンテキスト (HTTPS) でのみ利用できます。

メモ: この機能はウェブワーカー内で利用可能です。

has()CacheStorage インターフェイスのメソッドで、Cache オブジェクトが cacheName と一致する場合に true に解決される Promise を返します。

CacheStorage には、ウィンドウの Window.caches プロパティまたはワーカーの WorkerGlobalScope.caches を介してアクセスできます。

構文

js
has(cacheName)

引数

cacheName

文字列で、 CacheStorage で探している Cache オブジェクトの名前を表します。

返値

Promise で、キャッシュが存在する場合は true、存在しない場合は false に解決されます。

次の例では、最初に 'v1' というキャッシュが存在するかどうかを確認します。 その場合、アセットのリストを追加します。 そうでない場合、何らかのキャッシュセットアップ関数を実行します。

js
caches
  .has("v1")
  .then((hasCache) => {
    if (!hasCache) {
      someCacheSetupFunction();
    } else {
      caches.open("v1").then((cache) => cache.addAll(myAssets));
    }
  })
  .catch(() => {
    // ここで例外を処理します。
  });

仕様書

Specification
Service Workers
# cache-storage-has

ブラウザーの互換性

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
has

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

関連情報