{"slug": "your-on-device-ai-works-offline-does-your-web-app", "title": "Your On-Device AI Works Offline. Does Your Web App?", "summary": "A developer built a small React brief editor that separates three offline concerns: a versioned service-worker cache for the HTML, JavaScript and CSS shell, localStorage for the user's draft, and an optional on-device AI model checked independently. The example precaches a build-generated asset allowlist, fails installation if a required asset is missing, avoids skipWaiting() to prevent version mixing under an open editor, and warns that a service-worker registration or navigator.onLine is not proof the app or model is usable offline.", "body_md": "An on-device model can be ready without a network connection while the application around it still fails to reopen. The model works; the HTML, JavaScript, fonts, or saved input do not. From the user’s perspective, the feature is still offline and unavailable.\n\nAn offline promise therefore needs an interaction-level test. Close or reload the page without connectivity and check whether the person can recover their work, use the available features, and understand what is unavailable.\n\nThe companion implements a small React brief editor with local draft storage and a versioned application cache. Native AI remains optional and independently checked. The example does not queue bookings, synchronize accounts, or pretend that caching a shell turns a server-backed workflow into a complete offline application.\n\nThe browser can have an application shell without a usable model, or a usable model without the application shell. The user’s draft is a third concern.\n\n| Resource | Owner in this example | Readiness question | \n|---|---|---|\n| HTML, JavaScript, CSS | Application service worker | Can the editor reopen? | \n| Synthetic brief | Application local storage | Can the person recover the text? | \n| Built-in AI model | Browser | Can the selected API perform this task? | \n\nDo not display a single “Offline ready” badge based only on successful service-worker registration. Registration does not establish that the worker controls this page, that all required assets were cached, or that the model can run.\n\nSimilarly, `navigator.onLine` is a connectivity hint. It cannot establish that a particular model endpoint or application API is reachable.\n\nThe build script collects the emitted HTML, JavaScript, and CSS and creates a cache name from their content hash. The generated service worker precaches that allowlist during installation.\n\n``` js\nself.addEventListener(\"install\", event => {\n  event.waitUntil(\n    caches.open(CACHE).then(cache => cache.addAll(URLS))\n  );\n});\n```\n\n`CACHE` and `URLS` are build-generated values, not placeholders in the delivered worker. If installation cannot obtain a required asset, it should fail rather than advertise a complete shell.\n\nThe example handles the root navigation with a network request and a cached fallback. Versioned assets use the active shell cache. Requests outside the same-origin allowlist are left alone.\n\nThis restriction matters. A broad “cache every GET” policy can capture personalized responses, unbounded resources, and data whose freshness rules differ from static application code. The lab has no authenticated API responses to cache.\n\nA new worker cleans up older caches bearing this application’s prefix when it activates. It does not delete unrelated caches from the origin.\n\nThe example also avoids forcing every new worker to activate immediately with `skipWaiting()`. Replacing the worker beneath an open editor can mix versions or interrupt work. A larger application should offer a controlled update after the person has saved or exported their draft.\n\nKeep old assets available on the server for a suitable deployment window. A versioned cache helps a previously installed application, but it cannot rescue an asset that was never cached and has already been removed from the server.\n\n*Figure 1. Offline editing and offline inference require different resources.*\n\nThe demonstration stores a small synthetic brief in `localStorage`. Its write path catches failures and tells the person to copy the text before closing the page.\n\n```\ntry {\n  localStorage.setItem(\"web-ai-lab-brief\", text);\n} catch {\n  setStorage(\"Local saving failed. Copy the brief before closing.\");\n}\n```\n\nThis is intentionally modest. Storage is not a backup, a multi-device sync mechanism, or an appropriate default for every confidential document. Real products need a retention policy, deletion controls, and storage choices that fit their data and size requirements.\n\nFor a richer editor, IndexedDB may be more appropriate. Preserve the same user-facing rule: a failed write must not leave an unqualified “Saved” indicator on screen.\n\nThe lab’s service worker does not attempt to cache Chrome’s built-in model internals. The application asks the API about the requested task and handles preparation or unavailability through the normal UI.\n\nIf you serve your own model through a library such as LiteRT.js, those model files and runtime assets become part of your application’s delivery design. Give them explicit versions, integrity records, download behavior, and cache rules. The companion excludes its benchmark’s model and Wasm files from the small editor-shell cache so that opening the editor does not silently download the benchmark workload.\n\nChrome’s model-caching guidance discusses models served by applications. Apply it to that delivery path rather than assuming every built-in model is a resource your service worker controls.\n\nBuild the production bundle with `npm run build`, then serve it with `npm run preview`. The development server deliberately does not register the production worker.\n\nFirst load the application online, save a synthetic brief, and wait for the worker to become ready. Reload to establish control, then disable connectivity and reload again. Verify that the editor appears and that the saved text is correct.\n\nNow repeat in a fresh profile that has never visited the application. An offline first visit has no installed shell to use. That should be reported as an unsupported scenario, not folded into a returning-user success rate.\n\nThe browser script passed this controlled-worker reload case in Headless Chrome 153. After saving a synthetic brief and establishing worker control, it disabled connectivity, reloaded, and recovered the exact saved text. The evidence file and screenshot record that result. This verifies the editor and draft; model inference still requires its own native preparation and offline run.\n\nFinally test missing assets, storage failures, and a model that is unavailable even though the editor opens. A successful warm model call while the page remains open does not cover any of those scenarios.\n\nThe practical goal is continuity: people can recover their text and understand the remaining options. Which part of your application’s offline promise have you verified after a complete reload?", "url": "https://wpnews.pro/news/your-on-device-ai-works-offline-does-your-web-app", "canonical_source": "https://dev.to/gde/your-on-device-ai-works-offline-does-your-web-app-38kj", "published_at": "2026-11-27 15:09:00+00:00", "updated_at": "2026-11-27 15:16:15.901601+00:00", "lang": "en", "topics": ["artificial-intelligence", "ai-tools", "developer-tools", "ai-products"], "entities": ["React"], "also_reported_by": [], "takeaway": "Test offline readiness at the interaction level: a service-worker registration and navigator.onLine prove neither that the page shell is cached nor that the on-device model can run, so check each resource separately.", "recommendation": {"advice": "Verify offline readiness per resource — cached shell, saved draft, and model availability — instead of trusting service-worker registration or navigator.onLine, and defer worker activation until the user has saved their work.", "playbook": "production-ops", "playbook_url": "https://wpnews.pro/playbooks/production-ops"}, "alternates": {"html": "https://wpnews.pro/news/your-on-device-ai-works-offline-does-your-web-app", "markdown": "https://wpnews.pro/news/your-on-device-ai-works-offline-does-your-web-app.md", "text": "https://wpnews.pro/news/your-on-device-ai-works-offline-does-your-web-app.txt", "jsonld": "https://wpnews.pro/news/your-on-device-ai-works-offline-does-your-web-app.jsonld"}}