Varnish issue with dynamic page cache module

Created on 18 February 2022, over 2 years ago
Updated 25 June 2024, 1 day ago

Updated content not reflecting on front end for anonymous user if page have "X-drupal-dynamic-cache" header is set as "UNCACHEABLE"

1. Install dynamic page cache.
2. Set up varnish.
3. Pass "User" cache context to that page through custom code.
4. Now you will see "X-drupal-dynamic-cache" header as "UNCACHEABLE"
5. However, this page will be cached to varnish
6. Now try to update the content then cache for that page does not purge from varnish. So updated content will not reflect on frontend for anonymous until we clear the varnish cache directly from varnish.

If we have user or session cache context on a page then dynamic page cache module set "X-drupal-dynamic-cache" header as "UNCACHEABLE" due to which purge module does not able to set "X-Acquia-Site" header for that page.

If page does not have "X-Acquia-Site" header set then Varnish is not able to purge cache for that page on tag invalidation.

So can we create a patch for Dynamic Cache Module so that it will never set "X-drupal-dynamic-cache" header as "UNCACHEABLE" for anonymous user?

diff --git a/core/modules/dynamic_page_cache/src/EventSubscriber/DynamicPageCacheSubscriber.php b/core/modules/dynamic_page_cache/src/EventSubscriber/DynamicPageCacheSubscriber.php
index 22b51b9e..4e961b44 100644
--- a/core/modules/dynamic_page_cache/src/EventSubscriber/DynamicPageCacheSubscriber.php
+++ b/core/modules/dynamic_page_cache/src/EventSubscriber/DynamicPageCacheSubscriber.php
@@ -165,7 +165,8 @@ class DynamicPageCacheSubscriber implements EventSubscriberInterface {
     }
 
     // There's no work left to be done if this is an uncacheable response.
-    if (!$this->shouldCacheResponse($response)) {
+    // Do not set uncacheable for anonymous user.
+    if (!\Drupal::currentUser()->isAnonymous() && !$this->shouldCacheResponse($response)) {
       // The response is uncacheable, mark it as such.
       $response->headers->set(self::HEADER, 'UNCACHEABLE');
       return;
💬 Support request
Status

Needs work

Version

10.3 ✨

Component
Cache  →

Last updated 1 day ago

Created by

🇮🇳India vikas shishodia

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.69.0 2024