How can I prevent a specific node - or a list of specific nodes - being cached server-side in Drupal 8?

I have page that pulls a value out of a $_SESSION variable using a PHP text block, and it is being cached, which is obviously not what I want.

  • A php text block evaluates only the output, no cache data. So you probably need to add the cache data somewhere else, for example in a node preprocess or view alter hook. But then you can place the rest of the php code there as well. – 4k4 Mar 20 '17 at 17:20
  • See also some of the related questions like drupal.stackexchange.com/questions/151254/… which answer your question – Berdir Mar 20 '17 at 20:43

I fixed this by doing the following:

  • I appended a cache buster querystring parameter to the link to the page that needed to be kept out of cache (using the timestamp token + random number token)
  • Added the 'url.query_args' cache context to the required_cache_contexts in services.yml

It puts a bit more strain on the memory use of the cache than just keeping it out of cache altogether, but the \Drupal::service('page_cache_kill_switch')->trigger(); line did not do anything for me at all.

I think it makes sense for the querystring to be part of the default cache context anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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