Skip to content

Commit 95b3b50

Browse files
[bugfix] add mode: no-cors to fix CORS error when following GCS redirects (#6277)
Fixes CORS error when service worker follows redirects to GCS by using mode: 'no-cors' to allow cross-origin fetches without CORS headers. ┆Issue is synchronized with this [Notion page](https://www.notion.so/PR-6277-bugfix-add-mode-no-cors-to-fix-CORS-error-when-following-GCS-redirects-2976d73d36508101a4cbd7b59106dfc3) by [Unito](https://www.unito.io)
1 parent 936da14 commit 95b3b50

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

public/auth-sw.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,17 @@ self.addEventListener('fetch', (event) => {
7070
// Handle redirects to external storage (e.g., GCS signed URLs)
7171
if (response.type === 'opaqueredirect') {
7272
// 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
73+
// Re-fetch the original /api/view URL with redirect: 'follow' and mode: 'no-cors'
74+
// - mode: 'no-cors' allows cross-origin fetches without CORS headers (GCS doesn't have CORS)
75+
// - Returns opaque response, which works fine for images/videos/audio
76+
// - Browser will send auth headers to /api/view (same-origin)
77+
// - Browser will receive 302 redirect to GCS
78+
// - Browser will follow redirect using GCS signed URL authentication
7979
return fetch(event.request.url, {
8080
method: 'GET',
8181
headers: headers,
82-
redirect: 'follow'
82+
redirect: 'follow',
83+
mode: 'no-cors'
8384
})
8485
}
8586

0 commit comments

Comments
 (0)