Varnish issue with dynamic page cache module

Created on 18 February 2022, almost 3 years ago
Updated 24 January 2024, 11 months 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

11.0 🔥

Component
Cache 

Last updated 3 days 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.

  • 🇺🇸United States jakegibs617

    remade number 7 here. Not sure if it solves the problem, needs to be tested still.

  • Open in Jenkins → Open on Drupal.org →
    Environment: PHP 8.1 & pgsql-13.5
    last update 11 months ago
    Build Successful
  • Open in Jenkins → Open on Drupal.org →
    Environment: PHP 8.1 & pgsql-14.1
    last update 11 months ago
    Build Successful
  • 🇧🇪Belgium waropd

    Update patch #18 to work with Drupal 10.3

  • Status changed to Postponed: needs info 26 days ago
  • 🇳🇿New Zealand quietone

    A committer has explained this is a support request but then we have a patch suggesting this is a bug. Which is it? This was reported on Drupal 9, is this still a problem on Drupal 103+ or Drupal 11?

    Since we need more information to move forward with this issue, I am setting the status to Postponed (maintainer needs more info). If we don't receive additional information to help with the issue, it may be closed after three months.

    Thanks!

Production build 0.71.5 2024