@@ -222,17 +222,28 @@ private function addCacheHeadersToWebResponse(): void
222
222
$ this ->cacheDuration = $ this ->cacheDuration ?? Module::getInstance ()->getConfig ()->staticCacheDuration ;
223
223
$ headers = Craft::$ app ->getResponse ()->getHeaders ();
224
224
225
- $ cacheControl = Craft::$ app ->getResponse ()->getHeaders ()->get (
226
- HeaderEnum::CACHE_CONTROL ->value
227
- );
228
-
229
- // Copy the cache-control header to the cdn-cache-control header
230
- Craft::$ app ->getResponse ()->getHeaders ()->setDefault (
225
+ $ cacheControlDirectives = Collection::make ($ headers ->get (
226
+ HeaderEnum::CACHE_CONTROL ->value ,
227
+ first: false ,
228
+ ));
229
+
230
+ // Copy cache-control directives to the cdn-cache-control header
231
+ // @see https://developers.cloudflare.com/cache/concepts/cdn-cache-control/#header-precedence
232
+ $ staleWhileRevalidateDuration = Module::getInstance ()->getConfig ()->staleWhileRevalidateDuration ;
233
+ $ cdnCacheControlDirectives = $ cacheControlDirectives ->isEmpty ()
234
+ ? Collection::make ([
235
+ 'public ' ,
236
+ "max-age= $ this ->cacheDuration " ,
237
+ "stale-while-revalidate= $ staleWhileRevalidateDuration " ,
238
+ ])
239
+ : $ cacheControlDirectives ;
240
+
241
+ $ headers ->setDefault (
231
242
HeaderEnum::CDN_CACHE_CONTROL ->value ,
232
- $ cacheControl ?? " public, max-age= $ this -> cacheDuration " ,
243
+ $ cdnCacheControlDirectives -> implode ( ' , ' ) ,
233
244
);
234
245
235
- // Capture, remove any existing headers so we can prepare them
246
+ // Capture, remove any existing headers, so we can prepare them
236
247
$ existingTagsFromHeader = Collection::make ($ headers ->get (HeaderEnum::CACHE_TAG ->value , first: false ) ?? []);
237
248
$ headers ->remove (HeaderEnum::CACHE_TAG ->value );
238
249
$ this ->tags = $ this ->tags ->push (...$ existingTagsFromHeader );
0 commit comments