feat(pwa): adopt thread-ripper pattern with in-app install buttons, floating banner, and GET share_target

This commit is contained in:
chrisf committed 2026-10-22 19:57:22 -06:00
1 parent c739f74055
commit 4d4b8c860d
3 files changed
+148 -86

No files matched your search

+39 -34
View File
@@ -1,18 +1,20 @@
// Everclone PWA Service Worker (v1.1.0)
const CACHE_NAME = 'everclone-cache-v2';
const PRECACHE_URLS = [
// Everclone PWA Service Worker (v2.0.0)
const CACHE_NAME = 'everclone-cache-v3';
const STATIC_ASSETS = [
'/',
'/manifest.json',
'/icon.svg',
'/icon-192.png',
'/icon-512.png',
'/apple-touch-icon.png'
'/icon-maskable-192.png',
'/icon-maskable-512.png',
'/apple-touch-icon.png',
'/icon.svg'
];
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE_NAME).then((cache) => {
return cache.addAll(PRECACHE_URLS).catch(() => {});
return cache.addAll(STATIC_ASSETS).catch(() => {});
})
);
self.skipWaiting();
@@ -31,44 +33,47 @@ self.addEventListener('activate', (event) => {
self.addEventListener('fetch', (event) => {
// CRITICAL: Never intercept POST/PUT/DELETE requests!
// This ensures Android Web Share Target multipart POSTs stream directly to /api/hooks/note
if (event.request.method !== 'GET') {
return;
}
const url = new URL(event.request.url);
// Bypass API endpoints, hooks, and share endpoint to always get fresh data
if (url.pathname.startsWith('/api/') || url.pathname === '/share') {
// Bypass API endpoints and share endpoint
if (url.pathname.startsWith('/api/') || url.pathname.startsWith('/share')) {
return;
}
// Network-first strategy for navigation and static assets with cache fallback
// For page navigations (including share target queries like /?url=... or /?noteId=...)
if (event.request.mode === 'navigate') {
event.respondWith(
fetch(event.request).catch(() => {
return caches.match('/');
})
);
return;
}
// Stale-while-revalidate for static assets
event.respondWith(
fetch(event.request)
.then((networkResponse) => {
if (
networkResponse &&
networkResponse.status === 200 &&
(url.pathname === '/' || url.pathname.startsWith('/_next/static/'))
) {
const responseClone = networkResponse.clone();
caches.open(CACHE_NAME).then((cache) => {
cache.put(event.request, responseClone);
});
}
return networkResponse;
})
.catch(() => {
return caches.match(event.request).then((cachedResponse) => {
if (cachedResponse) {
return cachedResponse;
caches.match(event.request).then((cachedResponse) => {
const fetchPromise = fetch(event.request)
.then((networkResponse) => {
if (
networkResponse &&
networkResponse.status === 200 &&
(url.pathname === '/' || url.pathname.startsWith('/_next/static/'))
) {
const responseClone = networkResponse.clone();
caches.open(CACHE_NAME).then((cache) => {
cache.put(event.request, responseClone);
});
}
if (event.request.mode === 'navigate') {
return caches.match('/');
}
return new Response('Network error occurred', { status: 503, statusText: 'Offline' });
});
})
return networkResponse;
})
.catch(() => cachedResponse);
return cachedResponse || fetchPromise;
})
);
});