
Description
Make a Meta Display Glasses webapp work offline with a Service Worker + Cache API (precache the app shell, cache-first fetch, online/offline UI). Use when the user wants offline support or resilience to flaky Wi-Fi.
SKILL.md
Required reading
Before generating or modifying any code, read both:
../../references/display-guidelines.md../../references/performance-guidelines.md
These define the non-negotiable display physics, input model, and performance budgets for Meta Display Glasses webapps. Do not skip — generated UI that ignores these will fail on-device.
If these reference files are unavailable in an isolated eval, do not search /, home directories, or unrelated workspaces. Apply the requirements already present in this skill and continue.
Add Offline Support to Meta Display Glasses WebApp
Make a webapp keep working when the glasses lose Wi-Fi, using a standard Service Worker + Cache API. Precache the app shell on install, serve cache-first on fetch, clean old caches on activate, and reflect online/offline state in the UI. No SDK required.
The base offline Service Worker snippet already lives in ../../references/performance-guidelines.md — reuse it as your starting point.
Prerequisites
- Existing webapp created via
/create-webapp - Served over HTTPS — Service Workers and the Cache API only run in a secure context.
file://will not work. Use/publish-to-vercelfor an HTTPS URL.
How It Works
- The app shell registers
sw.js. - On
install, the worker precachesindex.html,app.js,styles.css, and any other shell assets. - On
fetch, the worker serves cache-first (cached response, falling back to network). - On
activate, it deletes stale caches when you bump the cache version. - The page reflects
navigator.onLineand theonline/offlineevents in the UI.
⚠️ Caveats (must-include)
- HTTPS-only (secure context). No exceptions.
- Precache real subresource URLs. List the actual files the page loads — don't assume the app's request interceptor will serve Service-Worker-originated fetches.
- Decide what should be available offline. Anything that needs fresh data from the internet won't work offline — show a friendly "offline" message in that case rather than failing silently.
- Storage is per-app. Pair this with
/add-local-storageso the wearer's settings/notes persist between sessions, not just the app shell. - Bump
CACHEversion when shell assets change, or wearers get stale files.
Steps
1. Register the Service Worker
In app.js (or an inline <script> in the shell):
if ('serviceWorker' in navigator) {
window.addEventListener('load', function () {
navigator.serviceWorker.register('/sw.js').catch(function (e) {
console.warn('SW registration failed', e); // app still works online
});
});
}
2. Create sw.js at the App Root
const CACHE = 'app-v1';
const FILES = ['/', '/index.html', '/app.js', '/styles.css', '/favicon.png'];
self.addEventListener('install', function (e) {
e.waitUntil(caches.open(CACHE).then(function (c) { return c.addAll(FILES); }));
});
self.addEventListener('activate', function (e) {
e.waitUntil(
caches.keys().then(function (keys) {
return Promise.all(keys.filter(function (k) { return k !== CACHE; })
.map(function (k) { return caches.delete(k); }));
})
);
});
self.addEventListener('fetch', function (e) {
e.respondWith(
caches.match(e.request).then(function (cached) {
return cached || fetch(e.request);
})
);
});
3. Reflect Online / Offline in the UI
Show or hide an offline indicator (e.g. #offline-banner) from navigator.onLine — build the indicator itself with /add-ui.
function updateOnlineUI() {
var offline = !navigator.onLine;
// Show/hide your offline indicator based on `offline` — see /add-ui
}
window.addEventListener('online', updateOnlineUI);
window.addEventListener('offline', updateOnlineUI);
updateOnlineUI();
4. Handle Data That Needs the Network
For screens that require fresh data, detect offline and show a message instead of a broken state:
if (!navigator.onLine) {
showToast('No connection — showing last saved data', 'error');
renderFromCache(); // e.g. /add-local-storage
return;
}
Verify
- App is served over HTTPS
-
navigator.serviceWorker.registerresolves (check DevTools / console) - App shell (
index.html,app.js,styles.css) is precached on install - Reloading offline still renders the app shell
- Old caches are cleaned when
CACHEversion bumps (activate handler) - Offline banner / message appears when connection drops
- Network-dependent screens degrade gracefully offline
Related Skills
/add-local-storage— Persist the wearer's data so it survives offline sessions/connect-api— Add cache-aware fallbacks around API calls/add-ui— Add the offline indicator
More skills from the meta-wearables-webapp repository
View all 12 skillsadd-device-sensors
integrate device sensor data
Sep 1FrontendHardwareIoTadd-gestures
handle gesture interactions in webapps
Sep 1DesignFrontendInteractionadd-local-storage
add client-side data persistence
Sep 1FrontendPersistenceStorageadd-text-input
add text input to webapps
Sep 1FormsFrontendUI Componentsadd-ui
add UI components to webapps
Sep 1FrontendJavaScriptReactUI Componentsconnect-api
connect webapps to REST APIs and WebSockets
Sep 1API DevelopmentFrontendREST APIWebSockets
More from Meta Open Source
View publisherrelay-best-practices
write idiomatic Relay code
relay
Apr 22EngineeringFrontendGraphQLReact +1relay-performance
optimize Relay application performance
relay
Jun 10FrontendGraphQLPerformanceReact +1add-shape-types-to-torch-model
annotate PyTorch models with tensor shapes
pyrefly
Jul 18Data ModelingDeep LearningPythonPyTorchcamera-streaming
configure camera streaming and photo capture
meta-wearables-dat-ios
Aug 6CameraHardwareiOSVideodat-conventions
develop iOS applications with DAT SDK
meta-wearables-dat-ios
Aug 6iOSMobileSDKSwiftdebugging
debug wearable device software
meta-wearables-dat-ios
Aug 6DebuggingEngineeringiOS