Banner shows page titles as "Access Denied" due to a block cache

Created on 15 January 2024, 11 months ago
Updated 1 April 2024, 9 months ago

Problem/Motivation

A few sites I support behind an Akamai caching service have reported having their H1 headings displaying as "Access Denied" to public users.

One site is running ver 1.4.5, the other I can't confirm but appears to be a newer version.

Steps to reproduce

  1. Using CivicTheme, as a content author, create a page in Draft.
  2. View the page at the live URL to trigger the page to be cached.
  3. Publish the page.
  4. Visit the page as a non-authenticated user

Proposed resolution

A potential fix could be adding a preprocess to include some CacheContext as exampled in this issue from Bootstrap: https://www.drupal.org/project/bootstrap/issues/3240459

🐛 Bug report
Status

Fixed

Version

1.4

Component

Code

Created by

🇦🇺Australia Toby Wild

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

Comments & Activities

  • Issue created by @Toby Wild
  • 🇦🇺Australia Toby Wild

    Adding a pre_process function to the theme should fix this.

    
    function <THEMENAME>_preprocess_block(&$variables) {
    
      if ($variables['configuration']['label'] && $variables['configuration']['label'] == 'Banner') {
        $variables['#cache']['contexts'][] = 'user';
      if ($variables['base_plugin_id'] != 'block_content') {
        return;
      }
    
      /** @var \Drupal\node\Entity\Node $node */
      $node = \Drupal::routeMatch()->getParameter('node');
    
      $block = $variables['elements']['content']['#block_content'];
    
      // If the node is not loaded, adjust the cache max-age of the banner block to -1.
      if ($block && $block->bundle() == 'civictheme_banner' && !$node) {
        $variables['#cache']['max-age'] = 0;
      }
    }
    
  • 🇦🇺Australia Toby Wild

    Clarifying my above suggestion:

    /**
     * Preprocess block function to add additional cache context on Banner block.
     */
    function <THEMENAME>_preprocess_block(&$variables) {
      if ($variables['base_plugin_id'] != 'block_content') {
        return;
      }
    
      $node = \Drupal::routeMatch()->getParameter('node');
      $block = $variables['elements']['content']['#block_content'];
    
      // If the node is not loaded, adjust the cache max-age of the banner block to -1.
      if ($block && $block->bundle() == 'civictheme_banner' && !$node) {
        $variables['#cache']['max-age'] = 0;
      }
    }
    
    
  • Assigned to joshua1234511
  • 🇦🇺Australia alex.skrypnyk Melbourne
  • Assigned to sonam.chaturvedi
  • Status changed to Needs review 10 months ago
  • 🇦🇺Australia alex.skrypnyk Melbourne
  • Assigned to alex.skrypnyk
  • Status changed to RTBC 9 months ago
  • 🇮🇳India sonam.chaturvedi Pune

    Verified and tested on 1.x-dev

    Testing Results:
    1 View the unpublished content as an anonymous user : The banner title shows Access denied - PASS
    2 View the published content as an anonymous user : The banner title does not show access denied and it shows page title - PASS

    Screenshots:
    unpublished content as an anonymous user

    published content as an anonymous user

  • Status changed to Fixed 9 months ago
  • 🇦🇺Australia alex.skrypnyk Melbourne

    Implemented, tested and will be released in 1.7

  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024