Skip to main content
@capgo/capacitor-asset-cache Files & Storage Open source

Asset Cache Capacitor plugin

Cache CDN images and videos in persistent app storage and bind them as local media sources

Install

bun add @capgo/capacitor-asset-cache bunx cap sync
npm, pnpm or yarn
  • npm install @capgo/capacitor-asset-cache
  • pnpm add @capgo/capacitor-asset-cache
  • yarn add @capgo/capacitor-asset-cache

Guide

How to use Asset Cache in Capacitor

Using @capgo/capacitor-asset-cache

@capgo/capacitor-asset-cache stores large CDN images and videos in app-owned persistent storage, then returns local source URLs for Capacitor webviews.

Install

npm install @capgo/capacitor-asset-cache
npx cap sync

What This Plugin Exposes

  • bind - Attach an image or video element to a local asset source and update it when ready.
  • src - Return a local source string after the asset exists locally.
  • resolve - Return the local source plus cache metadata.
  • configure - Set default CDN and revalidation behavior.
  • get, list, remove, clear, and getCacheSize - Manage the cache explicitly when needed.

Example Usage

Configure a CDN once, then bind elements by relative path:

import { AssetCache } from '@capgo/capacitor-asset-cache';

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

const hero = document.querySelector<HTMLImageElement>('#hero');

if (hero) {
  AssetCache.bind(hero, 'images/hero.jpg');
}

The element gets data-asset-cache-state="loading" while the native fetch runs, then receives the local source URL and moves to ready.

img[data-asset-cache-state='loading'],
video[data-asset-cache-state='loading'] {
  opacity: 0.45;
}

Protected Assets

Headers are sent by the plugin while it fetches or revalidates the remote file. The web element still receives only the local source URL.

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

Without A CDN Base URL

Pass an absolute URL when you do not want to configure cdnUrl.

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

Relative paths require either a configured cdnUrl or a per-call cdnUrl.

Storage Notes

On iOS, files live under Application Support and are excluded from iCloud backup. On Android, files live under the app internal files directory. The system does not treat these files as disposable cache files, but uninstall, app data clear, clear(), and remove() still delete them.

Full Reference

Keep going from Using @capgo/capacitor-asset-cache

If you are using Using @capgo/capacitor-asset-cache to plan persistent media loading, connect it with @capgo/capacitor-asset-cache for implementation details, Getting Started for implementation details, @capgo/capacitor-downloader for explicit background downloads, and @capgo/capacitor-file for general local file access.

FAQ

Asset Cache plugin FAQ

How do I install the Asset Cache plugin in a Capacitor app?

Run "bun add @capgo/capacitor-asset-cache" (or "npm install @capgo/capacitor-asset-cache"), then run "bunx cap sync" so the iOS and Android projects pick up the native code. Import it from "@capgo/capacitor-asset-cache" in your app code.

Does @capgo/capacitor-asset-cache work with React, Vue and Angular?

Yes. Asset Cache is a Capacitor package, so it works with any web framework that runs inside Capacitor, including Ionic, React, Vue, Angular, Svelte and plain JavaScript.

Which Capacitor version does @capgo/capacitor-asset-cache support?

Capgo plugins follow Capacitor's major version: use the plugin major version that matches your Capacitor major version (for example plugin v8 with Capacitor 8). The compatibility table in the GitHub README lists the maintained versions.

Is @capgo/capacitor-asset-cache free and open source?

Yes. The source code is public on GitHub at https://github.com/Cap-go/capacitor-asset-cache/ and the package is free to install from npm. Bug reports and pull requests are welcome.

Can I update code that uses Asset Cache without an App Store review?

Installing or upgrading the plugin changes native code, so it needs a new store build. After that, JavaScript, HTML and CSS changes that call the plugin can ship instantly with Capgo live updates.

Ship Asset Cache changes without waiting for app review

Once the plugin is in your store build, Capgo live updates push your JavaScript, HTML and CSS changes to users in minutes.

Start with Capgo