Skip to content

Commit 936da14

Browse files
[bugfix] fix service worker opaqueredirect error and ensure SW controls page before mount (#6275)
Fixes service worker network error by handling opaqueredirect responses correctly and ensures SW registration completes before app mount to prevent race conditions on first load. ┆Issue is synchronized with this [Notion page](https://www.notion.so/PR-6275-bugfix-fix-service-worker-opaqueredirect-error-and-ensure-SW-controls-page-before-mount-2976d73d36508106bc65dc82cdc62779) by [Unito](https://www.unito.io)
1 parent bab4786 commit 936da14

File tree

4 files changed

+24
-10
lines changed

4 files changed

+24
-10
lines changed

public/auth-sw.js

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,28 @@ self.addEventListener('fetch', (event) => {
6767
})
6868
)
6969

70-
// If redirected to external storage (GCS), follow without auth headers
71-
// The signed URL contains its own authentication in query params
72-
if (
73-
response.type === 'opaqueredirect' ||
74-
response.status === 302 ||
75-
response.status === 301
76-
) {
70+
// Handle redirects to external storage (e.g., GCS signed URLs)
71+
if (response.type === 'opaqueredirect') {
72+
// Opaqueredirect: redirect occurred but response is opaque (headers not accessible)
73+
// Re-fetch the original /api/view URL with redirect: 'follow'
74+
// Browser will:
75+
// 1. Send auth headers to /api/view (same-origin)
76+
// 2. Receive 302 redirect to GCS
77+
// 3. Automatically strip auth headers when following cross-origin redirect
78+
// 4. Use GCS signed URL authentication instead
79+
return fetch(event.request.url, {
80+
method: 'GET',
81+
headers: headers,
82+
redirect: 'follow'
83+
})
84+
}
85+
86+
// Non-opaque redirect (status visible) - shouldn't normally happen with redirect: 'manual'
87+
// but handle as fallback
88+
if (response.status === 302 || response.status === 301) {
7789
const location = response.headers.get('location')
7890
if (location) {
91+
// Follow redirect manually - do NOT include auth headers for external URLs
7992
return fetch(location, {
8093
method: 'GET',
8194
redirect: 'follow'

src/main.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,9 @@ app
8383
})
8484

8585
// Register auth service worker after Pinia is initialized (cloud-only)
86+
// Wait for registration to complete before mounting to ensure SW controls the page
8687
if (isCloud) {
87-
void import('@/platform/auth/serviceWorker')
88+
await import('@/platform/auth/serviceWorker')
8889
}
8990

9091
app.mount('#vue-app')

src/platform/auth/serviceWorker/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ import { isCloud } from '@/platform/distribution/types'
55
* Tree-shaken for desktop/localhost builds via compile-time constant.
66
*/
77
if (isCloud) {
8-
void import('./register')
8+
await import('./register')
99
}

src/platform/auth/serviceWorker/register.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,4 @@ function setupCacheInvalidation(): void {
5454
})
5555
}
5656

57-
void registerAuthServiceWorker()
57+
await registerAuthServiceWorker()

0 commit comments

Comments
 (0)