@@ -8,47 +8,64 @@ export default {
88 try {
99 const cacheInfo = JSON . parse ( cacheInfoHeader ) ;
1010 const modifiedResponse = new Response ( response . body , response ) ;
11+ const url = new URL ( request . url ) ;
12+ const ext = url . pathname . split ( '.' ) . pop ( ) . toLowerCase ( ) ;
1113
12- // Clear any existing cache headers that might have been set by IAP
13- modifiedResponse . headers . delete ( 'Pragma' ) ;
14- modifiedResponse . headers . delete ( 'Cache-Control' ) ;
15- modifiedResponse . headers . delete ( 'Expires' ) ;
16- modifiedResponse . headers . delete ( 'ETag' ) ;
17- modifiedResponse . headers . delete ( 'Last-Modified' ) ;
14+ // Clear any existing cache headers
15+ const headersToDelete = [
16+ 'Pragma' , 'Cache-Control' , 'Expires' , 'ETag' , 'Last-Modified' ,
17+ 'x-cloud-trace-context' , 'x-appengine-resource-usage' ,
18+ 'cf-cache-status' , 'cf-ray' , 'x-powered-by'
19+ ] ;
20+ headersToDelete . forEach ( header => modifiedResponse . headers . delete ( header ) ) ;
1821
1922 // Restore cache control headers
20- const { policy, expires, etag, lastModified } = cacheInfo ;
21- modifiedResponse . headers . set (
22- 'Cache-Control' ,
23- `${ policy . visibility } , max-age=${ policy . maxAge } , stale-while-revalidate=${ policy . staleWhileRevalidate } `
24- ) ;
25- modifiedResponse . headers . set ( 'Expires' , expires ) ;
26-
27- // Restore validation headers
28- if ( etag ) {
29- modifiedResponse . headers . set ( 'ETag' , etag ) ;
30- }
31- if ( lastModified ) {
32- modifiedResponse . headers . set ( 'Last-Modified' , lastModified ) ;
33- }
23+ const { policy, expires, etag, lastModified, contentLength } = cacheInfo ;
3424
3525 // Handle conditional requests
3626 const ifNoneMatch = request . headers . get ( 'If-None-Match' ) ;
3727 const ifModifiedSince = request . headers . get ( 'If-Modified-Since' ) ;
3828
39- if ( ( ifNoneMatch && etag === ifNoneMatch ) ||
40- ( ifModifiedSince && lastModified === ifModifiedSince ) ) {
29+ // Add immutable flag for certain file types
30+ const isImmutable = [ 'wasm' , 'data' ] . includes ( ext ) ;
31+ let cacheControl = `${ policy . visibility } , max-age=${ policy . maxAge } , stale-while-revalidate=${ policy . staleWhileRevalidate } ` ;
32+ if ( isImmutable ) {
33+ cacheControl += ', immutable' ;
34+ }
35+
36+ // Return 304 for conditional requests
37+ if ( ( etag && ifNoneMatch === etag ) ||
38+ ( lastModified && ifModifiedSince && new Date ( ifModifiedSince ) >= new Date ( lastModified ) ) ) {
4139 return new Response ( null , {
4240 status : 304 ,
4341 headers : new Headers ( {
44- 'Cache-Control' : modifiedResponse . headers . get ( 'Cache-Control' ) ,
42+ 'Cache-Control' : cacheControl ,
4543 'ETag' : etag ,
46- 'Last-Modified' : lastModified
44+ 'Last-Modified' : lastModified ,
45+ 'Content-Type' : response . headers . get ( 'Content-Type' ) ,
46+ 'Vary' : 'Accept-Encoding'
4747 } )
4848 } ) ;
4949 }
5050
51- // Remove the custom header since we've processed it
51+ // Set cache headers for normal responses
52+ modifiedResponse . headers . set ( 'Cache-Control' , cacheControl ) ;
53+ modifiedResponse . headers . set ( 'Expires' , expires ) ;
54+ modifiedResponse . headers . set ( 'Vary' , 'Accept-Encoding' ) ;
55+
56+ // Add compression hint for large files
57+ if ( contentLength > 1024 * 1024 ) {
58+ modifiedResponse . headers . set ( 'Accept-Encoding' , 'gzip, deflate, br' ) ;
59+ }
60+
61+ if ( etag ) {
62+ modifiedResponse . headers . set ( 'ETag' , etag ) ;
63+ }
64+ if ( lastModified ) {
65+ modifiedResponse . headers . set ( 'Last-Modified' , lastModified ) ;
66+ }
67+
68+ // Remove the custom header
5269 modifiedResponse . headers . delete ( 'X-Cache-Info' ) ;
5370 return modifiedResponse ;
5471 } catch ( e ) {
0 commit comments