Some checks failed
CD / Build and push images (push) Successful in 2m59s
CI / Lint, typecheck, test (push) Successful in 2m3s
CI / Auth e2e pack (push) Failing after 2m18s
CI / Build container images (push) Has been skipped
CD / Deploy to Test (push) Successful in 9s
CD / Smoke tests against Test (push) Successful in 1m32s
CD / Promote to Int (push) Successful in 12s
Editing continues without a connection and merges conflict-free on reconnect (ADR 0003, realtime-collaboration.md §Offline). - y-indexeddb mirrors every opened page's Y.Doc to IndexedDB, sharing the document with the collab provider. The local copy is discarded when the page is left after a successful server sync (bounding IndexedDB growth) and kept otherwise so offline edits survive to the next visit. - vite-plugin-pwa service worker precaches the app shell (build assets only) with a navigation fallback; `/api` and `/collab` are denylisted and there is no runtime caching, so API responses are never cached or poisoned. - Offline page resolution WITHOUT caching API responses: the app itself persists the small metadata it needs to reopen a visited page (page/pond ids + slugs, bounded LRU in localStorage) and the last signed-in user, so after an offline tab reload the app stays signed in, resolves the page, and restores its content from IndexedDB. Both are revalidated when the network returns (a 401 clears the cached user). - Local-only UI: a banner when there are edits held only on this device (provider `onUnsyncedChanges`), de + en. Tests: `page-cache` unit test (remember/recall + bounded eviction); a new `offline` e2e pack (validated locally against the full stack and wired into CI): edit, reload while offline (shell from the SW, content from IndexedDB), assert an API call fails offline (no SW API caching), then reconnect and a second client converges. The e2e static server serves `.webmanifest`. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PGdhRiwU1WRL4XxJfZYipY
69 lines
2.1 KiB
TypeScript
69 lines
2.1 KiB
TypeScript
/**
|
|
* A tiny app-level cache of the metadata needed to open a previously-visited
|
|
* page while offline (issue #38). It maps a page URL (pond slug + page slug) to
|
|
* the ids the editor and collab layer need. This is persisted by the app in
|
|
* localStorage — deliberately NOT by the service worker — so API responses are
|
|
* never cached, yet a visited page can still be resolved with no network.
|
|
*
|
|
* The store is bounded to the most recently visited pages so it cannot grow
|
|
* without limit; each entry is a few hundred bytes.
|
|
*/
|
|
|
|
const STORAGE_KEY = 'dorfteich:offline-pages';
|
|
const MAX_ENTRIES = 50;
|
|
|
|
export interface CachedPage {
|
|
pondSlug: string;
|
|
pondId: string;
|
|
pageSlug: string;
|
|
pageId: string;
|
|
title: string;
|
|
/** Last write time, for bounded recency-based eviction. */
|
|
savedAt: number;
|
|
}
|
|
|
|
type Store = Record<string, CachedPage>;
|
|
|
|
function keyOf(pondSlug: string, pageSlug: string): string {
|
|
return `${pondSlug}/${pageSlug}`;
|
|
}
|
|
|
|
function readStore(): Store {
|
|
try {
|
|
const raw = localStorage.getItem(STORAGE_KEY);
|
|
return raw ? (JSON.parse(raw) as Store) : {};
|
|
} catch {
|
|
return {};
|
|
}
|
|
}
|
|
|
|
function writeStore(store: Store): void {
|
|
try {
|
|
localStorage.setItem(STORAGE_KEY, JSON.stringify(store));
|
|
} catch {
|
|
// Best-effort: ignore quota errors or disabled storage.
|
|
}
|
|
}
|
|
|
|
/** Record the metadata of a page loaded online, for later offline resolution. */
|
|
export function rememberPage(entry: Omit<CachedPage, 'savedAt'>): void {
|
|
const store = readStore();
|
|
store[keyOf(entry.pondSlug, entry.pageSlug)] = { ...entry, savedAt: Date.now() };
|
|
|
|
const keys = Object.keys(store);
|
|
if (keys.length > MAX_ENTRIES) {
|
|
keys
|
|
.sort((a, b) => (store[a]?.savedAt ?? 0) - (store[b]?.savedAt ?? 0))
|
|
.slice(0, keys.length - MAX_ENTRIES)
|
|
.forEach((key) => delete store[key]);
|
|
writeStore(store);
|
|
} else {
|
|
writeStore(store);
|
|
}
|
|
}
|
|
|
|
/** Look up a previously-visited page's metadata by its URL slugs. */
|
|
export function recallPage(pondSlug: string, pageSlug: string): CachedPage | null {
|
|
return readStore()[keyOf(pondSlug, pageSlug)] ?? null;
|
|
}
|