Problem/Motivation

On ESI Block requests, there is an error in ESIBlockController.php
The two conditions for select cache context are never true because $type is a string but RequestHandler::CACHE_PER_ROLE and RequestHandler::CACHE_PER_USER are integer.

    // Depends on type set the cache context.
    $type = $settings['cache']['cachemode'];
    if ($type === RequestHandler::CACHE_PER_ROLE) { // FALSE
      $response->getCacheableMetadata()->addCacheContexts(['user.roles']);
    }
    elseif ($type === RequestHandler::CACHE_PER_USER) { // FALSE
      $response->getCacheableMetadata()->addCacheContexts(['user']);
    }

Proposed resolution

You should cast $type to int

    // Depends on type set the cache context.
    $type = $settings['cache']['cachemode'];
    if ((int)$type === RequestHandler::CACHE_PER_ROLE) {
      $response->getCacheableMetadata()->addCacheContexts(['user.roles']);
    }
    elseif ((int)$type === RequestHandler::CACHE_PER_USER) {
      $response->getCacheableMetadata()->addCacheContexts(['user']);
    }
CommentFileSizeAuthor
failEsiBlockCacheContext.png51.98 KBmounir_abid

Comments

mounir_abid created an issue. See original summary.

mounir_abid’s picture

Issue summary: View changes
mounir_abid’s picture

Issue summary: View changes
shumer’s picture

Assigned: Unassigned » shumer

  • shumer committed bc1ba8e on 8.x-3.x
    Issue #3208805 by mounir_abid, shumer: fail condition when add cache...

  • shumer committed 3aa7582 on 4.x
    Issue #3208805 by mounir_abid, shumer: fail condition when add cache...
shumer’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

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