Skip to content

Service Worker–Based Persistent Prefetch Cache #2

@hoangsonww

Description

@hoangsonww

Summary
Introduce a service worker layer to persist GhostIO’s prefetched data beyond the in-memory cache. This allows cached API responses to survive page reloads, browser restarts, and offline usage.


Why

  • Current GhostIO caching is in-memory only (lost on reload).
  • SPAs and dashboards often reload or navigate across routes → prefetched data is discarded.
  • Developers want persistent, offline-ready prefetching (PWA-style).
  • Aligns with GhostIO’s goal of improving UX speed + reliability.

Scope (MVP)

  1. Service Worker Integration

    • Register a service worker that listens for GhostIO-prefetched requests.
    • Store prefetched responses in the Cache Storage API.
  2. Persistent Cache Retrieval

    • When ghost.get(url) is called:

      • First check in-memory cache.
      • If not found, check service worker cache.
      • If found, hydrate into memory and return.
  3. Configurable Cache Policy

    • persistPrefetch (default: false).
    • cacheTTL option: expiry time for persistent entries.
    • maxPersistentSize: optional limit for storage size.
  4. Offline Support

    • If user is offline, GhostIO can still serve cached data from service worker.

Example Usage

import { GhostIO } from "ghost-io";

const ghost = new GhostIO({
  persistPrefetch: true,
  cacheTTL: 1000 * 60 * 60, // 1 hour
});

// Prefetch API data
ghost.prefetch("/api/dashboard");

// Later, even after reload:
const data = await ghost.get("/api/dashboard");
console.log(data); // served from service worker cache if available

Acceptance Criteria

  • Prefetched data persists across reloads and browser restarts.
  • Expired cache entries are evicted according to TTL.
  • Works seamlessly with existing registerAxios integration.
  • Fallback to live network fetch if no cache available.

Tasks

  • Add service worker registration + setup.
  • Implement persistPrefetch and cacheTTL config options.
  • Extend ghost.get(url) to check persistent cache.
  • Add eviction policy for expired items.
  • Write Jest/e2e tests for persistence + offline scenarios.
  • Update README with usage + config examples.

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingdocumentationImprovements or additions to documentationduplicateThis issue or pull request already existsenhancementNew feature or requestgood first issueGood for newcomershelp wantedExtra attention is neededquestionFurther information is requested

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions