@@ -238,7 +238,7 @@ class CloudflareAdapter {
238
238
if ( this . cookiesToSetRequest . length > 0 ) {
239
239
newRequest = this . setMultipleReqSerializedCookies ( newRequest , this . cookiesToSetRequest ) ;
240
240
}
241
-
241
+
242
242
// Add additional headers from cdnSettings if present
243
243
if ( cdnSettings . additionalHeaders && typeof cdnSettings . additionalHeaders === 'object' ) {
244
244
this . logger . debug ( 'Adding additional headers from cdnSettings' ) ;
@@ -320,9 +320,12 @@ class CloudflareAdapter {
320
320
*/
321
321
async handleFetchFromOrigin ( request , originUrl , cdnSettings , ctx ) {
322
322
this . logger . debug ( `Handling fetch from origin [handleFetchFromOrigin]: ${ originUrl } ` ) ;
323
- let response = undefined ;
324
- let cacheKey = undefined ;
323
+ let response ;
324
+ let cacheKey ;
325
325
const clonedRequest = this . cloneRequestWithNewUrl ( request , originUrl ) ;
326
+ const shouldUseCache =
327
+ this . coreLogic . requestConfig ?. overrideCache !== true && cdnSettings ?. cacheRequestToOrigin === true ;
328
+
326
329
this . eventListenersResult = await this . eventListeners . trigger ( 'beforeCreateCacheKey' , request , this . result ) ;
327
330
if ( this . eventListenersResult && this . eventListenersResult . cacheKey ) {
328
331
cacheKey = this . eventListenersResult . cacheKey ;
@@ -331,16 +334,36 @@ class CloudflareAdapter {
331
334
this . eventListenersResult = await this . eventListeners . trigger ( 'afterCreateCacheKey' , cacheKey , this . result ) ;
332
335
}
333
336
this . logger . debug ( `Generated cache key: ${ cacheKey } ` ) ;
337
+
334
338
const cache = caches . default ;
335
339
this . eventListenersResult = await this . eventListeners . trigger (
336
340
'beforeReadingCache' ,
337
341
request ,
338
342
this . requestConfig ,
339
343
this . result
340
344
) ;
341
- if ( ( this . coreLogic . requestConfig ?. overrideCache === true ) && cdnSettings ?. cacheRequestToOrigin === true ) {
345
+
346
+ if ( shouldUseCache ) {
342
347
response = await cache . match ( cacheKey ) ;
348
+ this . logger . debug ( `Cache ${ response ? 'hit' : 'miss' } for key: ${ cacheKey } ` ) ;
349
+ }
350
+
351
+ if ( ! response ) {
352
+ this . logger . debug ( `Fetching fresh content from origin: ${ originUrl } ` ) ;
353
+ const newRequest = this . abstractionHelper . abstractRequest . createNewRequestFromUrl ( originUrl , clonedRequest ) ;
354
+ response = await this . fetchFromOriginOrCDN ( newRequest ) ;
355
+
356
+ if ( shouldUseCache && response . ok ) {
357
+ // Only cache the response if caching is enabled and the response is successful
358
+ response = this . abstractionHelper . abstractResponse . createNewResponse ( response . body , response ) ;
359
+ if ( ! response . headers . has ( 'Cache-Control' ) ) {
360
+ response . headers . set ( 'Cache-Control' , 'public' ) ;
361
+ }
362
+ await this . cacheResponse ( ctx , cache , cacheKey , response , cdnSettings . cacheTTL ) ;
363
+ this . logger . debug ( `Cached fresh content for key: ${ cacheKey } ` ) ;
364
+ }
343
365
}
366
+
344
367
this . eventListenersResult = await this . eventListeners . trigger (
345
368
'afterReadingCache' ,
346
369
request ,
@@ -352,30 +375,6 @@ class CloudflareAdapter {
352
375
response = this . eventListenersResult . modifiedResponse ;
353
376
}
354
377
355
- if ( ! response ) {
356
- this . logger . debug ( `Cache miss for ${ originUrl } . Fetching from origin.` ) ;
357
- const newRequest = this . abstractionHelper . abstractRequest . createNewRequestFromUrl ( originUrl , clonedRequest ) ;
358
- response = await this . fetchFromOriginOrCDN ( newRequest ) ;
359
- if ( response . headers . has ( 'Cache-Control' ) ) {
360
- response = this . abstractionHelper . abstractResponse . createNewResponse ( response . body , response ) ;
361
- response . headers . set ( 'Cache-Control' , 'public' ) ;
362
- }
363
- if ( response . ok ) {
364
- this . cacheResponse ( ctx , cache , cacheKey , response , cdnSettings . cacheTTL = null ) ;
365
- this . eventListenersResult = await this . eventListeners . trigger (
366
- 'afterCacheResponse' ,
367
- request ,
368
- response ,
369
- this . result
370
- ) ;
371
- if ( this . eventListenersResult && this . eventListenersResult . modifiedResponse ) {
372
- response = this . eventListenersResult . modifiedResponse ;
373
- }
374
- }
375
- } else {
376
- this . logger . debug ( `Cache hit for: ${ originUrl } with cacheKey: ${ cacheKey } ` ) ;
377
- }
378
-
379
378
return this . applyResponseSettings ( response , cdnSettings ) ;
380
379
}
381
380
@@ -435,28 +434,28 @@ class CloudflareAdapter {
435
434
try {
436
435
let urlToFetch = typeof input === 'string' ? input : input . url ;
437
436
this . logger . debug ( 'urlToFetch:' , urlToFetch ) ;
438
-
437
+
439
438
// Parse the original URL
440
439
const originalUrl = new URL ( urlToFetch ) ;
441
-
440
+
442
441
// Check if the URL is for the Optimizely datafile
443
442
if ( ! originalUrl . hostname . includes ( 'cdn.optimizely.com' ) ) {
444
443
// Create a new URL using this.pagesUrl as the base
445
444
const newUrl = new URL ( this . pagesUrl ) ;
446
-
445
+
447
446
// Preserve the original path and search parameters
448
447
newUrl . pathname = originalUrl . pathname ;
449
448
newUrl . search = originalUrl . search ;
450
-
449
+
451
450
urlToFetch = newUrl . toString ( ) ;
452
-
451
+
453
452
if ( urlToFetch . endsWith ( '/' ) ) {
454
453
urlToFetch = urlToFetch . slice ( 0 , - 1 ) ;
455
454
}
456
455
}
457
-
456
+
458
457
this . logger . debug ( `Fetching from origin or CDN [fetchFromOriginOrCDN]: ${ urlToFetch } ` , options ) ;
459
-
458
+
460
459
const response = await AbstractRequest . fetchRequest ( urlToFetch , options ) ;
461
460
this . logger . debug ( `Fetching from origin or CDN [fetchFromOriginOrCDN] - response` , response ) ;
462
461
return response ;
@@ -617,19 +616,19 @@ class CloudflareAdapter {
617
616
async getDatafile ( sdkKey , ttl = 3600 ) {
618
617
this . logger . debugExt ( `Getting datafile [getDatafile]: ${ sdkKey } ` ) ;
619
618
const url = `https://cdn.optimizely.com/datafiles/${ sdkKey } .json` ;
620
-
619
+
621
620
try {
622
- const response = await this . fetchFromOriginOrCDN ( url , {
621
+ const response = await this . fetchFromOriginOrCDN ( url , {
623
622
cf : { cacheTtl : ttl } ,
624
623
headers : {
625
- 'Content-Type' : 'application/json'
626
- }
624
+ 'Content-Type' : 'application/json' ,
625
+ } ,
627
626
} ) ;
628
-
627
+
629
628
if ( ! response . ok ) {
630
629
throw new Error ( `Failed to fetch datafile: ${ response . statusText } ` ) ;
631
630
}
632
-
631
+
633
632
return await response . text ( ) ;
634
633
} catch ( error ) {
635
634
this . logger . error ( `Error fetching datafile for SDK key ${ sdkKey } : ${ error } ` ) ;
0 commit comments