Skip to content

Cache the web card database in IndexedDB, gated on latest.json - #105

Open
Manwe-777 wants to merge 1 commit into
devfrom
web-cards-db-cache
Open

Cache the web card database in IndexedDB, gated on latest.json#105
Manwe-777 wants to merge 1 commit into
devfrom
web-cards-db-cache

Conversation

@Manwe-777

@Manwe-777 Manwe-777 commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

What

The web build downloads the ~5MB en-database.sqlite.gz from the Supabase mirror on every page load. latest.json is fetched, but only to check that the release publishes SQLite — it is never compared against anything, there is no local cache, and the mirror answers Cache-Control: no-cache so the browser's HTTP cache revalidates as well. The Electron build has always had the version-gated disk cache; the web path lost that behavior in the SQLite migration.

This gives the web the same flow against IndexedDB:

  • The inflated database is stored in the existing mtgatool-local KV store under cardsDb:{lang} together with the version it holds.
  • Each boot fetches only the tiny latest.json; the payload moves solely when the published version is newer than the cached one.
  • Unreachable latest.json or a failed download serves the cached copy (a version behind beats no cards); a corrupt cache entry is ignored via the SQLite-header check; a broken IndexedDB falls through to the network, never blocking the cards.
  • The worker source label distinguishes cache:vN from mirror:vN.

Verified

  • Live against npm run start:web: after the first (seeding) load, a reload's only request to the Supabase bucket is latest.json — no .gz — and the worker boots from the cached bytes.
  • New spec fetchDatabaseWeb.spec.ts covers the decision paths: up-to-date cache skips the payload entirely, offline falls back to the cache, no-cache-no-network yields null, corrupt cache is ignored.
  • The transfer of dbBytes to the worker is safe with the cache: the structured clone into IndexedDB happens at kvPut before the buffer is transferred/detached.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features

    • Added reliable web caching for card databases, reducing unnecessary downloads.
    • Uses cached data when release metadata, downloads, decompression, or validation are unavailable.
    • Records the database source and language-specific file details.
  • Bug Fixes

    • Rejects invalid cached database files.
    • Gracefully returns no database when neither cached nor network data is available.

The web build downloaded the ~5MB en-database.sqlite.gz from the
Supabase mirror on every page load: latest.json was read only to check
that the release publishes SQLite, never compared against anything, and
the mirror answers Cache-Control: no-cache so the browser revalidates
too. The Electron build has always version-checked against its disk
cache; the web path lost that in the SQLite migration.

fetchDatabaseWeb now mirrors ensureDatabaseFile against IndexedDB: the
inflated database lives in the mtgatool-local KV store keyed by lang
with the version it holds, each boot moves only the tiny latest.json,
and the payload downloads solely when the published version is newer.
An unreachable latest.json or a failed download serves the cached copy —
a version behind is far better than no cards — a corrupt cache entry is
ignored via the SQLite-header check, and a broken IndexedDB falls
through to the network. The source label distinguishes cache:vN from
mirror:vN.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Aug 14, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: eb655eab-58cc-468f-9e73-10e810779c10

📥 Commits

Reviewing files that changed from the base of the PR and between 4cd4bfd and 2e39f9f.

📒 Files selected for processing (3)
  • src/utils/cardsDb/__tests__/fetchDatabaseWeb.spec.ts
  • src/utils/cardsDb/fetchCardsDb.ts
  • src/utils/cardsDb/loadCardsDbBytes.ts

📝 Walkthrough

Walkthrough

Changes

Web database cache

Layer / File(s) Summary
Cache storage and result contract
src/utils/cardsDb/fetchCardsDb.ts
Added IndexedDB helpers that accept only valid SQLite bytes. Added the WebDatabase result shape with database bytes and source metadata.
Cached retrieval and mirror fallback
src/utils/cardsDb/fetchCardsDb.ts, src/utils/cardsDb/__tests__/fetchDatabaseWeb.spec.ts
Updated fetchDatabaseWeb to use current cached data, handle metadata and mirror failures, validate downloads, persist successful data, and report its source. Added tests for these paths.
Database loader source propagation
src/utils/cardsDb/loadCardsDbBytes.ts
Updated loadWeb to consume the new result shape and construct the language-specific source path.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Merge Risk: ⚪ Minimal · up to 2e39f

The change adds version-gated IndexedDB caching for the web card database with fallback behavior, and no actionable merge-blocking risk remains beyond normal checks and review.

Sequence Diagram(s)

sequenceDiagram
  participant loadWeb
  participant fetchDatabaseWeb
  participant IndexedDB
  participant ReleaseMetadata
  participant Mirror
  loadWeb->>fetchDatabaseWeb: Request database
  fetchDatabaseWeb->>IndexedDB: Read cached SQLite entry
  fetchDatabaseWeb->>ReleaseMetadata: Read release metadata
  fetchDatabaseWeb->>Mirror: Download and inflate payload
  Mirror-->>fetchDatabaseWeb: Return validated SQLite bytes
  fetchDatabaseWeb->>IndexedDB: Store bytes and release version
  fetchDatabaseWeb-->>loadWeb: Return bytes and source
Loading
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: caching the web card database in IndexedDB with latest.json version gating.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

Comment @coderabbitai help to get the list of available commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant