added service worket to set COOP AND COEP headers#2
Conversation
|
Warning Rate limit exceeded
⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughAdds a best-effort Cross-Origin-Isolation shim: a service worker at Changes
Sequence DiagramsequenceDiagram
participant Browser as Browser/App
participant SW as COI Service Worker
participant Network as Network
Browser->>SW: register('/sw-coi.js', {scope: '/'})
SW-->>Browser: registration ready
Browser->>Browser: check cross-origin isolation
alt not cross-origin isolated
Browser->>Browser: reload page
end
Browser->>Network: GET (same-origin)
Network-->>SW: fetch intercepted
SW->>Network: fetch original request
Network-->>SW: original Response
SW->>SW: proxy response, inject COOP/COEP/CORP if missing
SW-->>Browser: modified Response
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
src/public/sw-coi.js (1)
29-36: Consider adding error handling for network failures.If
fetch(request)throws (e.g., network offline), the error propagates up and the service worker fails the request. While this is standard behavior, you may want explicit error handling to fall back gracefully or provide clearer diagnostics.💡 Optional: Add try-catch for network errors
event.respondWith( (async () => { - const networkResponse = await fetch(request); + let networkResponse; + try { + networkResponse = await fetch(request); + } catch { + // Network error - let the browser handle it normally + return fetch(request); + } // Do not attempt to rewrite opaque or redirected responses.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/public/sw-coi.js` around lines 29 - 36, Wrap the fetch(request) call inside the async handler passed to event.respondWith in a try-catch so network failures are handled explicitly: catch exceptions from fetch(request) and return a fallback Response or a cached response (or a descriptive error Response) instead of letting the error bubble; update the logic around networkResponse (the variable returned by fetch) so the opaque/opaqueredirect check only runs when networkResponse was obtained successfully and ensure the catch block logs or reports the error for diagnostics.src/page_assets/index/js/main.js (1)
18-20: Consider auto-reload strategy for first-visit COI activation.
registerCoiServiceWorker()is called withoutawait, andinit()runs immediately. This is fine for startup performance, but there's a timing consideration: service workers cannot intercept the navigation that registered them, socrossOriginIsolatedwon't betrueuntil the next page load.On first visit in production (without server-side headers from vite), OPFS will be unavailable until the user refreshes. If guaranteed COI on first visit is needed, consider checking and reloading:
💡 Auto-reload pattern for first-visit COI
async function registerCoiServiceWorker() { if (!('serviceWorker' in navigator)) return; + + // Already isolated - no need to register shim + if (window.crossOriginIsolated) return; try { await navigator.serviceWorker.register('/sw-coi.js', { scope: '/' }); await navigator.serviceWorker.ready; + // SW active but page loaded without COI headers - reload to apply + if (!window.crossOriginIsolated) { + window.location.reload(); + } } catch { // Ignore registration failures; app can still run without SW header shim. } } // Start the app -registerCoiServiceWorker(); -init(); +registerCoiServiceWorker().then(() => init());Note: If the current "best-effort" behavior is intentional (avoiding surprise reloads), the existing implementation is acceptable—just be aware OPFS won't work on first visit without server-side headers.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/page_assets/index/js/main.js` around lines 18 - 20, The app currently calls registerCoiServiceWorker() without awaiting and immediately runs init(), which means crossOriginIsolated may not be true on first visit; update the startup in main.js to wait for the COI registration/activation result from registerCoiServiceWorker() (or have it return a status/promise), then check crossOriginIsolated and, if registration succeeded but crossOriginIsolated is still false, trigger a single reload to let the newly-active COI headers take effect before calling init(); use the existing registerCoiServiceWorker() return value and the crossOriginIsolated global to decide whether to reload once, then call init() after reload or when COI is active.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@src/page_assets/index/js/main.js`:
- Around line 18-20: The app currently calls registerCoiServiceWorker() without
awaiting and immediately runs init(), which means crossOriginIsolated may not be
true on first visit; update the startup in main.js to wait for the COI
registration/activation result from registerCoiServiceWorker() (or have it
return a status/promise), then check crossOriginIsolated and, if registration
succeeded but crossOriginIsolated is still false, trigger a single reload to let
the newly-active COI headers take effect before calling init(); use the existing
registerCoiServiceWorker() return value and the crossOriginIsolated global to
decide whether to reload once, then call init() after reload or when COI is
active.
In `@src/public/sw-coi.js`:
- Around line 29-36: Wrap the fetch(request) call inside the async handler
passed to event.respondWith in a try-catch so network failures are handled
explicitly: catch exceptions from fetch(request) and return a fallback Response
or a cached response (or a descriptive error Response) instead of letting the
error bubble; update the logic around networkResponse (the variable returned by
fetch) so the opaque/opaqueredirect check only runs when networkResponse was
obtained successfully and ensure the catch block logs or reports the error for
diagnostics.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 46665dab-248e-4631-8253-c4c554ba8d4a
📒 Files selected for processing (2)
src/page_assets/index/js/main.jssrc/public/sw-coi.js
|
add serviceworkers |
Summary by CodeRabbit