1- import process from "node:process" ;
2-
3- // TODO: Add more CDNs
41/** The CDN that the cache headers are being used with. Will work with other CDNs, but may miss platform-specific headers and directives. */
52export type CDN =
63 | "netlify"
74 | "cloudflare"
85 | "akamai"
96 | "vercel"
7+ | "fastly"
108 | ( string & { } ) ;
119
1210/** Number of seconds in one minute */
@@ -28,20 +26,17 @@ const cdnCacheControlHeaderNames = new Map<CDN, string>([
2826 [ "cloudflare" , "Cloudflare-CDN-Cache-Control" ] ,
2927 [ "akamai" , "Akamai-Cache-Control" ] ,
3028 [ "vercel" , "Vercel-CDN-Cache-Control" ] ,
29+ [ "fastly" , "Cache-Control" ] ,
3130] ) ;
3231
3332function detectCDN ( ) : CDN | undefined {
34- if ( process . env . CDN ) {
35- return process . env . CDN as CDN ;
33+ if ( globalThis ?. process ? .env ? .CDN ) {
34+ return globalThis . process . env . CDN as CDN ;
3635 }
37- if ( process . env . VERCEL ) {
36+ if ( globalThis ?. process ? .env . VERCEL ) {
3837 return "vercel" ;
3938 }
40- if (
41- process . env . NETLIFY ||
42- process . env . NETLIFY_LOCAL ||
43- process . env . NETLIFY_BLOBS_CONTEXT
44- ) {
39+ if ( "Netlify" in globalThis ) {
4540 return "netlify" ;
4641 }
4742
@@ -98,10 +93,15 @@ export class CacheHeaders extends Headers {
9893 if ( this . #cdn === "netlify" ) {
9994 cdnDirectives [ tieredDirective ] = "" ;
10095 }
101- this . setCdnCacheControl ( cdnDirectives ) ;
10296
103- directives . public = "" ;
104- delete directives [ "s-maxage" ] ;
97+ // If the CDN cache-control header is the same as the browser cache-control header, we merge the directives.
98+ if ( this . cdnCacheControlHeaderName === "Cache-Control" ) {
99+ Object . assign ( directives , cdnDirectives ) ;
100+ } else {
101+ this . setCdnCacheControl ( cdnDirectives ) ;
102+ delete directives [ "s-maxage" ] ;
103+ directives . public = "" ;
104+ }
105105
106106 if ( ! directives [ "max-age" ] ) {
107107 directives [ "max-age" ] = "0" ;
@@ -208,6 +208,8 @@ export class CacheHeaders extends Headers {
208208 switch ( this . #cdn) {
209209 case "netlify" :
210210 return "Netlify-Cache-Tag" ;
211+ case "fastly" :
212+ return "Surrogate-Key" ;
211213 default :
212214 return "Cache-Tag" ;
213215 }
0 commit comments