Why do returning users of a Transformers.js page keep running old weights after the model repo's main branch changed, and how do you control that cache?
answer
- the cache answers before the network
- a hit is never revalidated
- the key is the resolve URL
- revision is part of that URL
- pin a commit, then clear old entries
basics
~20 sTransformers.js checks its Cache API store before any request, keyed by the resolve URL including the revision, and never revalidates a hit, so files cached from main stay. Pin revision to a commit, bump it for new weights, clear old entries.
solid answer
~50 sIn the browser, Transformers.js opens a Cache API store named by `env.cacheKey` (default `'transformers-cache'`) and checks it before any request; a hit is used as is, with no revalidation. The key is the file's remote URL, `https://huggingface.co/<model>/resolve/<revision>/<file>`, and `revision` defaults to `'main'`, so a new commit on `main` has the same URL and returning users keep the old bytes until the entry is deleted. Make the version explicit: pass `revision` as a commit sha, or a tag you never move, and bump it when you ship new weights; a new URL is a cache miss and a fresh download. Then remove the old copy with `ModelRegistry.clear_pipeline_cache(task, model, { revision: old, dtype })`. In Node the same lookup goes to a file-system cache under `env.cacheDir`; `env.useBrowserCache = false` turns the browser cache off, and `env.useCustomCache` with `env.customCache` swaps in your own store.
go deeper
Know that Transformers.js caches model files in the browser, so a second visit skips the download.
Explain the lookup order and the cache key: the resolve URL with the revision inside it, used without revalidation.
Make model rollout deterministic: pin a commit sha, bump it to release, clear old revisions with ModelRegistry, and expect cache writes to fail quietly under storage pressure.
Treat client-side model versions like deployed assets: decide who bumps the pin, how old revisions are evicted, and whether mixed versions across users are acceptable during a rollout.
## How a file is looked up Every file a Transformers.js pipeline needs, from `config.json` to the ONNX weights, goes through the same loader: 1. **Pick a cache backend.** Among the main options, in order of priority: your own store if `env.useCustomCache` is true (it must implement the Web Cache API's `match` and `put`), then the browser's Cache API if `env.useBrowserCache` is true (the default wherever `caches` exists, including workers), then a file-system cache if `env.useFSCache` is true (the default in Node). 2. **Look the file up.** The loader asks the cache for the file's key. On a **hit** it uses the stored response as is. No request goes to the Hub, and nothing checks whether the remote file has changed. 3. **On a miss, download and store.** The file is fetched from the Hub and written to the cache for next time. The browser store is opened with `caches.open(env.cacheKey)`, and `env.cacheKey` defaults to `'transformers-cache'`. How the Cache API itself matches and stores responses is general browser-storage material; what matters here is the key Transformers.js uses. ## Why `main` goes stale | Environment | Store | Key for a file | |---|---|---| | Browser or worker | Cache API, `transformers-cache` | `https://huggingface.co/<model>/resolve/<revision>/<file>` | | Node, `revision` = `'main'` | files under `env.cacheDir` | `<model>/<file>` | | Node, other revision | files under `env.cacheDir` | `<model>/<revision>/<file>` | `revision` defaults to `'main'`. A new commit pushed to `main` changes the bytes behind the URL, not the URL, and therefore not the key. A returning visitor's loader finds the old entry, uses it, and never asks the Hub. Two users can run different weights under the same code until one of them clears site data. ## Making the version explicit - **Pin `revision` to a commit sha.** A commit is immutable, so the key and the bytes always agree. - **A tag works if it never moves.** A branch name other than `main` has the same problem as `main`: new pushes keep the old key. - **Ship new weights by bumping the pinned value.** The new key misses, the new files download, and the old entries simply stop being read. - **Pin `model` and `dtype` too.** The file list, and therefore every key, depends on them. ## Cleaning up and checking Old entries are not deleted when you bump the revision; they keep using storage. Transformers.js 4.3's `ModelRegistry` handles this per model: - `ModelRegistry.is_pipeline_cached(task, model, options)` returns whether every file for that task, model, revision and dtype is cached. - `ModelRegistry.clear_pipeline_cache(task, model, { revision: oldRevision, dtype })` deletes those files and reports how many it removed. Storage limits are enforced by the browser. If a cache write fails, for example on a full quota, Transformers.js catches the error and logs `Unable to add response to browser cache`; the pipeline still loads from the downloaded bytes, but the next visit downloads again. ## Turning caching off or replacing it - `env.useBrowserCache = false` stops using the Cache API, so every load downloads; it does not delete existing entries. - `env.useCustomCache = true` with `env.customCache` set to an object that implements `match` and `put` routes all caching through your own store. - In Node, `env.cacheDir` (or the `cache_dir` option of `pipeline()`) moves the file-system cache. ## A release checklist for new weights 1. Push the new ONNX files to the model repo and note the resulting commit sha. 2. Change the pinned `revision` in the app to that sha and deploy. 3. After the new pipeline is `ready`, call `ModelRegistry.clear_pipeline_cache` with the same task and dtype and the previous sha, so returning users do not keep both copies. 4. Keep the previous sha written down; rolling back is the same change in reverse. ## Edge cases worth knowing - **Private windows in iframes.** Opening the Cache API can throw a security error there; Transformers.js logs a warning and continues without a cache, so every load downloads. - **Non-HTTP origins.** The Cache API accepts only `http` and `https` keys, so files bundled inside a browser extension are not cached.
- Does pinning a Transformers.js pipeline to a branch other than `main` avoid the stale-cache problem?No. The cache key contains the branch name, not a commit, so new pushes to that branch keep the old key and returning users keep the old bytes. Pin an immutable commit sha, or a tag you never move, and change the pinned value to roll out new weights.
- A user's browser storage is full. Does Transformers.js `pipeline()` fail to load?No. The download succeeds; the failed cache write is caught and logged as a warning, `Unable to add response to browser cache`, and the pipeline is built from the downloaded bytes. The cost appears on the next visit, which downloads again. Clearing stale revisions with `ModelRegistry.clear_pipeline_cache` frees space.
Caching under the main URL is like a library that shelves a book as 'latest edition' and, once a copy is on the shelf, never asks the publisher again. Only a request for a specific printing, a commit, guarantees which copy you are handed.
- Transformers.js revalidates cached files against the Hub on every load
- Transformers.js keeps model files in IndexedDB
- Leaving revision at main makes loads reproducible
- A full storage quota makes pipeline() throw
- Setting useBrowserCache to false deletes the cached files
Assumes Transformers.js 4.3; the ModelRegistry cache helpers and the cache-key rules were verified against that release.