0

I wrote a block in Drupal 8 to render a menu based on the http headers of the request, which contains the users' information from the idp. This menu is supposed to be different from user to user, but currently it is cached once it is generated when the very first user login to the website.

I have tried setting the cache max-age to 0, but it didn't seem to work at all. Then I tried the session of the cache contexts, but still those pages visited by previous users are cached. Neither did header:custid.

Here is a sample code

class MyBlock extends BlockBase{
  public function build () {
    return array(
      '#cache'=>array('contexts'=>array('session')),
     '#markup' => ...,
    );
  }

 ...
}

Thanks in advance for any help!

1
  • This is because page_cache module caches complete pages for anonymous users. If you want to have this for them as well and not jsut for logged in users, where dynamic_page_cache comes to play, and still keep catching then you have to do it via JS. If this happens just for some anonymous users then you can implement page request/response policy tagged service that will stop page_cache from serving cached content and instead the dynamic_page_cache will be used.
    – user21641
    Commented Jan 21, 2016 at 7:55

3 Answers 3

1

I wrote a block in Drupal 8 to render a menu based on the http headers of the request, which contains the users' information from the idp.

If you want to build a new page for every request then there is no point in using the page cache, so I would uninstall the core module Internal Page Cache.

Edit: Just read in drupal planet, BigPipe will be available in Drupal 8.1, then you have a perfect solution for this. But in the meantime running a site without page cache for anonymous users is fine, you still have the dynamic cache and can use the cache tags.

0

This is because page_cache module caches complete pages for anonymous users. If you want to have this for them as well and not jsut for logged in users, where dynamic_page_cache comes to play, and still keep catching then you have to do it via JS. If this happens just for some anonymous users then you can implement page request/response policy tagged service that will stop page_cache from serving cached content and instead the dynamic_page_cache will be used. See the headers on page request and look for x-drupal-cache and x-dynamic-page-cache headers(they will have hit, miss, uncacheable or they won't be present).

0

As you can read in this post you should implement the following methods to override the default cache configuration for custom blocks:

  • defaultConfiguration()
  • getCacheContexts()
  • getCacheTags()
  • getCacheMaxAge()

In your case, you need to add this code to your block:

 /**
  * {@inheritdoc}
  */
  public function getCacheContexts() {
    return ['session'];
  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.