Skip to content

Commit 1d7bdc0

Browse files
rosaclaude
andcommitted
Fix networkFirst handler re-caching responses from cache
The networkFirst handler was calling saveToCache for ALL responses, including those retrieved from the cache itself. When a cached opaque response was re-saved after being read with a different request mode (e.g., redirect: 'manual' from Turbo), the response type could change from 'opaque' to 'opaqueredirect', corrupting the cached entry. Now we only cache responses that actually came from the network. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent f70788a commit 1d7bdc0

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/offline/handlers/network_first.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export class NetworkFirst extends Handler {
1010
let afterHandlePromise
1111
let timeoutId
1212
let cacheAttemptedOnTimeout = false
13+
let responseFromNetwork = false
1314

1415
const networkPromise = this.fetchFromNetwork(request)
1516
const promises = [networkPromise]
@@ -32,6 +33,10 @@ export class NetworkFirst extends Handler {
3233
// Either the network wins or the timeout wins. The network might win
3334
// with an error, though
3435
response = await Promise.race(promises)
36+
// If we got here without timeout, response is from network
37+
if (!cacheAttemptedOnTimeout) {
38+
responseFromNetwork = true
39+
}
3540
} catch(error) {
3641
console.warn(
3742
`${error} fetching from network ${request.url} with timeout`
@@ -46,6 +51,7 @@ export class NetworkFirst extends Handler {
4651
// anything to lose, knowing that we don't have the cache as fallback
4752
try {
4853
response = await networkPromise
54+
responseFromNetwork = true
4955
} catch(error) {
5056
// This might be the same error we got from the promise race
5157
console.warn(
@@ -58,7 +64,8 @@ export class NetworkFirst extends Handler {
5864
response = await this.fetchFromCache(request)
5965
}
6066

61-
if (response) {
67+
// Only cache responses that came from the network, not from cache
68+
if (response && responseFromNetwork) {
6269
afterHandlePromise = this.saveToCache(request, response.clone())
6370
} else {
6471
afterHandlePromise = Promise.resolve()

0 commit comments

Comments
 (0)