はじめに
インストール手順とフル マークダウン ガイドを含むセットアップの質問をコピーします。
Set up this Capacitor plugin in the project.
Use the package manager already used by the project.
Install these package(s): `@capgo/capacitor-asset-cache`
Run the required Capacitor sync/update step after installation.
Read this markdown guide for the full setup steps: https://raw.githubusercontent.com/Cap-go/website/refs/heads/main/apps/docs/src/content/docs/docs/plugins/asset-cache/getting-started.mdx
Use that guide for platform-specific steps, native file edits, permissions, config changes, imports, and usage setup.
If that guide references other docs pages, read them too.
インストール
「インストール」をクリック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-cachenpx cap syncインポート
「インポート」をクリックimport { AssetCache } from '@capgo/capacitor-asset-cache';CDNを設定
セクション: CDNを設定アプリが起動したときに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_5__
フレームワークの状態の直接ソース__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 | キャッシュされたローカルファイルは、明示的に削除するまで十分です。 |
ttl | CDNが安定したETagヘッダーを返す場合など、簡単な新鮮さのウィンドウが必要です。 |
always | CDNがETagヘッダーを返さない場合など、毎回リモートアセットを確認したい場合。 |
etag | CDNが安定した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' },});フル リファレンス
完全なリファレンス- GitHub https://github.com/Cap-go/capacitor-asset-cache/
- パッケージ:
@capgo/capacitor-asset-cache