Skip to content

はじめに

GitHub

CapgoのAIアシスタントを使用してプラグインをインストールすることができます。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';

アプリが起動したときにCDNの基本URLを一度設定します。相対的なアセットパスは、このURLに対して解決されます。

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

スキップすることができます cdnUrl 絶対URLを渡す場合、または bind, srcクリップボードにコピー resolve.

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

bind クリップボードにコピー

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;
}

__CAPGO_KEEP_2__

__CAPGO_KEEP_3__
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" />;
}

__CAPGO_KEEP_4__

__CAPGO_KEEP_3__
<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>

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

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

__CAPGO_KEEP_0__を使用します。必要な場合、メタデータも必要です。 resolve コピー

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

コピー

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

Protected Assets

デフォルトの戦略を設定するか、1つのアセットにのみオーバーライドする configure コピー

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

利用可能な戦略:

戦略使用する場合
neverキャッシュされたローカルファイルは、明示的に削除するまで十分です。
ttlCDNが安定したETagヘッダーを返す場合など、簡単な新鮮さのウィンドウが必要です。
alwaysCDNがETagヘッダーを返さない場合など、毎回リモートアセットを確認したい場合。
etagCDNが安定したETagヘッダーを返す場合など、CDNがETagヘッダーを返さない場合など。
last-modified__CAPGO_KEEP_0__

データストレージとプライバシー

データストレージとプライバシー
  • iOSは、Application Supportにファイルを格納し、iCloudバックアップからキャッシュルートを除外します。
  • Androidは、内部ファイルディレクトリにアプリケーションファイルを格納します。
  • Web uses the browser Cache API and localStorage metadata as a development fallback.
  • キャッシュファイルは、ユーザーメディアに保存されず、パブリックにアクセスできないアプリケーションサンドボックス内に隠されています。
  • キャッシュファイルはアンインストール、データクリア、またはプラグインの場合に削除されます。 clear() キャッシュは永続的ですが、セキュリティの境界ではありません。 remove().

高度なキャッシュ制御

高度なキャッシュ制御

__CAPGO_KEEP_0__

ほとんどのUI code は bind, src, または resolve. 以下のヘルパーを使用する必要がある場合は、明示的なキャッシュ管理を実行します:

メソッド説明
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' },
});

フル リファレンス

完全なリファレンス