@@ -16,7 +16,19 @@ const assetPath = (url) => {
1616 return url . origin === origin ? url . pathname : null ;
1717} ;
1818const networkUrl = ( path ) => assetUrls [ path ] || path ;
19- const pending = new Map ( ) ;
19+ // Wait before fetching instead of retaining response clones behind disk writes.
20+ // Waiting requests hold metadata only; every asset remains eligible offline.
21+ const fillWaiters = [ ] ;
22+ let activeFills = 0 ;
23+ async function acquireFill ( ) {
24+ if ( activeFills >= 2 ) await new Promise ( ( resolve ) => fillWaiters . push ( resolve ) ) ;
25+ else activeFills ++ ;
26+ return ( ) => {
27+ const next = fillWaiters . shift ( ) ;
28+ if ( next ) next ( ) ;
29+ else activeFills -- ;
30+ } ;
31+ }
2032let writes = Promise . resolve ( ) ;
2133let inventory ;
2234const keyFor = ( path ) => `${ origin } ${ path } ?${ REVISION } =${ ASSET_REVISIONS [ path ] } ` ;
@@ -186,6 +198,7 @@ self.addEventListener("fetch", (event) => {
186198 }
187199 if ( ! Object . hasOwn ( ASSET_REVISIONS , path ) ) return ;
188200 let cacheHit = false ;
201+ let releaseFill ;
189202 const response = ( async ( ) => {
190203 try {
191204 if ( SHELL_URLS . includes ( path ) ) {
@@ -205,39 +218,66 @@ self.addEventListener("fetch", (event) => {
205218 } catch {
206219 /* Cache API can be unavailable even when workers are enabled. */
207220 }
208- let network = pending . get ( path ) ;
209- if ( ! network ) {
210- // Public CDN responses must be readable, including CSS/background images,
211- // so the same byte verification and offline cache work for every asset.
221+ releaseFill = await acquireFill ( ) ;
222+ try {
223+ // Another request may have filled this revision while admission waited.
224+ try {
225+ const hit = await ( await caches . open ( ASSET_CACHE ) ) . match ( keyFor ( path ) ) ;
226+ if ( hit ) {
227+ cacheHit = true ;
228+ releaseFill ( ) ;
229+ return hit ;
230+ }
231+ } catch {
232+ /* Storage failure must still allow network delivery. */
233+ }
234+ // Public CDN responses must be readable for verification and offline caching.
212235 const readable = logicalPaths . has ( url . href )
213236 ? new Request ( request , { mode : "cors" , credentials : "omit" } )
214237 : request ;
215- network = fetch ( readable ) . catch ( ( error ) => {
238+ try {
239+ return await fetch ( readable ) ;
240+ } catch ( error ) {
216241 if ( readable === request ) throw error ;
217- return fetch ( readable , { cache : "reload" } ) ;
218- } ) ;
219- pending . set ( path , network ) ;
220- void network . finally ( ( ) => pending . delete ( path ) ) . catch ( ( ) => { } ) ;
242+ return await fetch ( readable , { cache : "reload" } ) ;
243+ }
244+ } catch ( error ) {
245+ releaseFill ( ) ;
246+ throw error ;
221247 }
222- return ( await network ) . clone ( ) ;
223248 } ) ( ) ;
224249 let cacheWork ;
225250 const delivered = response . then ( ( result ) => {
226251 // Cached bodies can be hundreds of MiB per boot. Don't tee them or perform
227252 // a second disk lookup when there is nothing to populate.
228253 if ( cacheHit ) return result ;
229254 // Clone before resolving respondWith; the browser may consume its body immediately.
230- const copy = result . clone ( ) ;
255+ let copy ;
256+ try {
257+ copy = result . clone ( ) ;
258+ } catch ( error ) {
259+ releaseFill ( ) ;
260+ throw error ;
261+ }
231262 cacheWork = ( async ( ) => {
232263 const cache = await caches . open ( ASSET_CACHE ) ;
233- if ( SHELL_URLS . includes ( path ) || ( await cache . match ( keyFor ( path ) ) ) ) return ;
234264 writes = writes
235- . then ( ( ) => storeAsset ( cache , path , copy ) )
265+ . then ( async ( ) => {
266+ if ( SHELL_URLS . includes ( path ) || ( await cache . match ( keyFor ( path ) ) ) ) return ;
267+ await storeAsset ( cache , path , copy ) ;
268+ } )
236269 . catch ( ( ) => {
237270 inventory = undefined ;
238271 } ) ;
239272 await writes ;
240- } ) ( ) . catch ( ( ) => { } ) ;
273+ } ) ( )
274+ . catch ( ( ) => { } )
275+ . finally ( ( ) => {
276+ // Discard unread branches after denial, duplicate hits or invalid data.
277+ // Do not await cancellation: the client owns the other tee branch.
278+ void copy . body ?. cancel ( ) . catch ( ( ) => { } ) ;
279+ releaseFill ( ) ;
280+ } ) ;
241281 return result ;
242282 } ) ;
243283 event . respondWith ( delivered ) ;
0 commit comments