|
| 1 | +// Disables access to DOM typings like `HTMLElement` which are not available |
| 2 | +// inside a service worker and instantiates the correct globals |
| 3 | +/// <reference no-default-lib="true"/> |
| 4 | +/// <reference lib="esnext" /> |
| 5 | +/// <reference lib="webworker" /> |
| 6 | + |
| 7 | +// Ensures that the `$service-worker` import has proper type definitions |
| 8 | +/// <reference types="@sveltejs/kit" /> |
| 9 | + |
| 10 | +// Only necessary if you have an import from `$env/static/public` |
| 11 | +/// <reference types="../.svelte-kit/ambient.d.ts" /> |
| 12 | + |
| 13 | +import { build, files, version } from '$service-worker'; |
| 14 | +// import { PUBLIC_BACKEND_HOST, PUBLIC_BACKEND_INSECURE } from '$env/static/public'; |
| 15 | +const PUBLIC_BACKEND_HOST = null; |
| 16 | +const PUBLIC_BACKEND_INSECURE = null; |
| 17 | + |
| 18 | +// This gives `self` the correct types |
| 19 | +const self = globalThis.self as unknown as ServiceWorkerGlobalScope; |
| 20 | + |
| 21 | +const dev = self.location.hostname === 'localhost'; |
| 22 | + |
| 23 | +// Force service worker to activate immediately and claim all clients |
| 24 | +self.addEventListener('install', (event) => { |
| 25 | + self.skipWaiting(); // Force activate new service worker immediately |
| 26 | +}); |
| 27 | + |
| 28 | +self.addEventListener('activate', (event) => { |
| 29 | + event.waitUntil(self.clients.claim()); // Take control of all clients immediately |
| 30 | +}); |
| 31 | + |
| 32 | +// Switch this to your production domain like api.example.com. |
| 33 | +// For now this is the backend dev server. |
| 34 | +const productionHost = PUBLIC_BACKEND_HOST || 'localhost:5174'; |
| 35 | +// Switch this to true if your production domain has TLS. |
| 36 | +const productionSecure = !PUBLIC_BACKEND_INSECURE || !dev; |
| 37 | + |
| 38 | +self.addEventListener('fetch', (event) => { |
| 39 | + const url = new URL(event.request.url); |
| 40 | + if (!url.pathname.startsWith('/_app/remote/')) return; |
| 41 | + |
| 42 | + const newUrl = new URL(event.request.url); |
| 43 | + newUrl.host = dev ? 'localhost:5174' : productionHost; |
| 44 | + newUrl.protocol = productionSecure ? 'https:' : 'http:'; |
| 45 | + |
| 46 | + async function respond() { |
| 47 | + try { |
| 48 | + const req = event.request.clone(); |
| 49 | + const headers = new Headers(req.headers); |
| 50 | + // Always mark as remote function call to ensure CORS headers |
| 51 | + headers.set('X-SvelteKit-Remote', 'true'); |
| 52 | + |
| 53 | + const init: RequestInit = { |
| 54 | + method: req.method, |
| 55 | + headers, |
| 56 | + credentials: 'include', |
| 57 | + referrer: req.referrer, |
| 58 | + referrerPolicy: req.referrerPolicy, |
| 59 | + redirect: req.redirect, |
| 60 | + cache: req.cache, |
| 61 | + }; |
| 62 | + |
| 63 | + if (req.method !== 'GET' && req.method !== 'HEAD') { |
| 64 | + // Use arrayBuffer for binary-safe forwarding, then convert back |
| 65 | + const bodyBuffer = await req.arrayBuffer(); |
| 66 | + init.body = bodyBuffer.byteLength > 0 ? bodyBuffer : null; |
| 67 | + } |
| 68 | + |
| 69 | + const response = await fetch(newUrl.toString(), init); |
| 70 | + |
| 71 | + // Validate response before returning |
| 72 | + if (!response.ok) { |
| 73 | + console.error( |
| 74 | + `[SW] Backend error ${response.status}: ${response.statusText} for ${newUrl}` |
| 75 | + ); |
| 76 | + return new Response( |
| 77 | + JSON.stringify({ |
| 78 | + type: 'error', |
| 79 | + status: response.status, |
| 80 | + error: `Backend server error: ${response.statusText}`, |
| 81 | + }), |
| 82 | + { |
| 83 | + status: response.status, |
| 84 | + headers: { 'Content-Type': 'application/json' }, |
| 85 | + } |
| 86 | + ); |
| 87 | + } |
| 88 | + |
| 89 | + return response; |
| 90 | + } catch (error) { |
| 91 | + console.error(`[SW] Network error for ${newUrl}:`, error); |
| 92 | + return new Response( |
| 93 | + JSON.stringify({ |
| 94 | + type: 'error', |
| 95 | + status: 503, |
| 96 | + error: 'Backend server unreachable', |
| 97 | + }), |
| 98 | + { |
| 99 | + status: 503, |
| 100 | + headers: { 'Content-Type': 'application/json' }, |
| 101 | + } |
| 102 | + ); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + event.respondWith(respond()); |
| 107 | +}); |
0 commit comments