コンテンツにジャンプ

はじめに

GitHub

AI-Assisted セットアップを使用してプラグインをインストールできます。AIツールに Capgo スキルを追加するには、以下のコマンドを実行してください。

ターミナルウィンドウ
npx skills add https://github.com/Cap-go/capgo-skills --skill capacitor-plugins

次に、以下のプロンプトを使用してください:

Use the `capacitor-plugins` skill from `Cap-go/capgo-skills` to install the `@capgo/capacitor-asset-cache` plugin in my project.

Manual Setup を使用する場合は、次のコマンドを実行してプラグインをインストールしてください:

ターミナル画面
npm install @capgo/capacitor-asset-cache
npx cap sync
import { AssetCache } from '@capgo/capacitor-asset-cache';

アプリが起動したときに 1 回だけ CDN のベース URL を設定してください。相対的なアセットのパスは、この URL に基づいて解決されます。

AssetCache.configure({
cdnUrl: 'https://cdn.example.com/assets/',
revalidate: {
strategy: 'ttl',
maxAgeSeconds: 86400,
},
});

スキップできます cdnUrl 絶対URLを渡す場合 bind, srcresolve.

const src = await AssetCache.src('https://cdn.example.com/assets/videos/intro.mp4');

イメージまたはビデオをバインド

「イメージまたはビデオをバインド」セクション

bind 最も透明な表示パスです。要素をロード中としてマークし、ローカルファイルが利用可能になるまでネイティブでアセットを取得または再検証し、ローカルファイルが利用可能になったらローカルソースURLを割り当てます。

const image = document.querySelector<HTMLImageElement>('#hero');
if (image) {
const binding = AssetCache.bind(image, 'images/hero.jpg');
await binding.promise;
}
<img id="hero" alt="Product preview" />
img[data-asset-cache-state='loading'],
video[data-asset-cache-state='loading'] {
opacity: 0.45;
}
img[data-asset-cache-state='ready'],
video[data-asset-cache-state='ready'] {
opacity: 1;
}
import { useEffect, useRef } from 'react';
import { AssetCache } from '@capgo/capacitor-asset-cache';
AssetCache.configure({ cdnUrl: 'https://cdn.example.com/assets/' });
export function HeroImage() {
const imageRef = useRef<HTMLImageElement>(null);
useEffect(() => {
if (!imageRef.current) return;
const binding = AssetCache.bind(imageRef.current, 'images/hero.jpg');
return () => binding.cancel();
}, []);
return <img ref={imageRef} alt="Product preview" />;
}
<script setup lang="ts">
import { onMounted, onUnmounted, ref } from 'vue';
import { AssetCache, type AssetCacheBinding } from '@capgo/capacitor-asset-cache';
const image = ref<HTMLImageElement | null>(null);
let binding: AssetCacheBinding | undefined;
onMounted(() => {
if (image.value) {
binding = AssetCache.bind(image.value, 'images/hero.jpg', {
cdnUrl: 'https://cdn.example.com/assets/',
});
}
});
onUnmounted(() => binding?.cancel());
</script>
<template>
<img ref="image" alt="Product preview" />
</template>

フレームワークの状態の直接ソース

「フレームワークの状態の直接ソース」

使用 src フレームワークがロードとエラー状態を制御している場合に使用します。ローカルファイルが存在する後、ローカルソース文字列のみがPromiseを解決します。

const src = await AssetCache.src('videos/intro.mp4');
videoElement.src = src;

使用する resolve メタデータも必要な場合:

const source = await AssetCache.resolve('images/hero.jpg');
console.log(source.src, source.local, source.fromCache, source.status);

ネイティブのfetchにヘッダーを渡す。有効なCDNパス、認証済みメディア、または短期間のトークンに便利。

const src = await AssetCache.src('private/videos/intro.mp4', {
cdnUrl: 'https://cdn.example.com/assets/',
headers: {
Authorization: `Bearer ${token}`,
},
});

プラグインはリモートファイルを取得または再検証する際にヘッダーを使用しますが、画像またはビデオタグはプラグインによって返されたローカルソースURLのみを受け取ります。

デフォルトの戦略を設定するには「または」1つのアセットにそれを上書きします。 configure Pass headers to the native fetch. This is useful for signed CDN paths, authenticated media, or short-lived tokens.

await AssetCache.src('videos/intro.mp4', {
revalidate: {
strategy: 'etag',
},
});

__CAPGO_KEEP_0__

__CAPGO_KEEP_0____CAPGO_KEEP_0__
never__CAPGO_KEEP_0__
ttl__CAPGO_KEEP_0__
always__CAPGO_KEEP_0__
etag__CAPGO_KEEP_0__
last-modified__CAPGO_KEEP_0__

__CAPGO_KEEP_0__

__CAPGO_KEEP_0__
  • 「Storage And Privacy」セクション
  • Androidはアプリの内部ファイルディレクトリにファイルを保存します。
  • WebはブラウザのキャッシュAPIとlocalStorageのメタデータを開発用のフォールバックとして使用します。
  • キャッシュされたファイルはアプリのサンドボックス内に隠されており、パブリックユーザーメディアに保存されません。
  • キャッシュされたファイルはアンインストール、データクリア、またはプラグイン「または」削除されます。 clear() キャッシュは永続的ですが、セキュリティ境界ではありません。機密情報が読み取られないようにするには、機密情報を暗号化してサーバーから提供する必要があります。 remove().

高度なキャッシュ制御

セクション「高度なキャッシュ制御」

ほとんどのUI__CAPGO_KEEP_0__は

Most UI code should use bind, srcキャッシュ管理を明示的に必要な場合にのみ、次のヘルパーを使用してください。 resolveメソッド

Advanced Cache Control説明
getリモート URL をネイティブのファイルメタデータに解決します。
listディスク上にまだ存在するキャッシュされたアセットを返します。
getCacheSizeキャッシュされたバイトの合計を返します。
removeキーまたは URL で指定されたキャッシュされたアセットを 1 つ削除します。
clearプラグインによって管理されているすべてのキャッシュされたアセットを削除します。
const asset = await AssetCache.get({
url: 'https://cdn.example.com/assets/videos/intro.mp4',
key: 'videos/intro.mp4',
revalidate: { strategy: 'last-modified' },
});