0

I'm showing my users a random number ({{ random() }}) in a twig which is being rendered in a view. But when I refresh a page it just shows me the same number.

I tried several things but they all failed:

  1. Disabled caching in admin/config/development/performance
  2. Disabled cache in views advanced settings
  3. tried to use cacheexclude module
  4. I even disabled twig cache!

How can I completely disable cache for specefic twig or specefic view?

Update:

By the way in response header there is a header called x-drupal-cache:HIT. (It's MISS for the first time and then HIT after first load) Why?

Update 2: I uninstalled Internal Page Cache too. the x-drupal-cache is gone now but the page is still being cached! I literally disabled everything but still no luck to show a random number in twig in a view..

This is my response header if it helps:

cache-control:must-revalidate, no-cache, private
content-encoding:gzip
content-type:text/html; charset=UTF-8
date:Wed, 28 Feb 2018 14:26:47 GMT
expires:Sun, 19 Nov 1978 05:00:00 GMT
server:nginx
status:200
strict-transport-security:max-age=15768000
vary:Accept-Encoding
x-content-type-options:nosniff
x-drupal-dynamic-cache:UNCACHEABLE
x-frame-options:SAMEORIGIN
x-generator:Drupal 8 (https://www.drupal.org)
x-ua-compatible:IE=edge

Solution:

I uninstalled Internal Cache module and ALSI big pipe module too.

  • 1 and 2 should be enough ... maybe your server is running some kind of cache ? its been know with some very popular hosts to run cache for an account automatically and you have to actually go in the panel to disable it. – GiorgosK Feb 28 '18 at 13:08
  • @GiorgosK I'm just using Nginx and php-fpm and nothing else... In response header there is a tag called x-drupal-cache:HIT – Sky Feb 28 '18 at 13:50
2

Unfortunately point 1 doesn't work for anonymous traffic.

You have to uninstall the Internal Page Cache module.

Or trigger

\Drupal::service('page_cache_kill_switch')->trigger();

in a view hook.

When the View is placed on most or all pages then uninstall the Internal Page Cache, because the page cache kill switch also disables the dynamic page cache and you don't want to disable both caches, unless this is only for a few pages which don't have too much traffic.

Additionally you need point 2 (disabled cache in views advanced settings) in any case.

Update:

By the way in response header there is a header called x-drupal-cache:HIT. (It's MISS for the first time and then HIT after first load) Why?

This header is generated by the Internal Page Cache, a HIT means it is served from this cache.

  • @4x4 are you sure ? I have at least a view on Drupal 3.7 currently that serves random content from a view (even for anonymous) with internal Page Cache enabled as I described in my answer. – GiorgosK Feb 28 '18 at 13:47
  • Thank you for your answer. Where should I add \Drupal::service('page_cache_kill_switch')->trigger();? mytheme_views? – Sky Feb 28 '18 at 13:54
  • @GiorgosK, yes, see this issue drupal.org/project/drupal/issues/2352009. Sky, you can use any view hook, but I would prefer hook_views_pre_render, because this also works in themes. – 4k4 Feb 28 '18 at 14:00
  • @4k4 I disabled Internal Page Cache Module. Also added \Drupal::service('page_cache_kill_switch')->trigger(); too. The x-drupal-cache header is gone but the page is still being cached :( I just did everything! – Sky Feb 28 '18 at 14:34
  • @Sky, to be sure you can add $variables['#cache'] = ['max-age' => 0]; to hook_preprocess_views_view, although this should be done already by point 2 in Views UI. I don't know what you can else do, at least not with the information you've provided so far. – 4k4 Feb 28 '18 at 14:50
1

In Drupal 8 in order to disable cache for a specific view

your view > ADVANCED > Caching > None

For your case twig caching is probably at play but it better be left untouched.

services.yml > twig.config:  cache: false

You might want to consider creating a random number in view using https://www.drupal.org/project/views_random_seed

NOTE: If you Drupal cache is enabled in admin/config/development/performance than the no cache setting of any view will not be respected by default and all views will be cached instead. Installing cache_control_override will help resolve this and allow views configured with no cache to work as such. Read more on this problem in the module's page.

Above problem is resolved assuming there is no other caching mechanisms between the drupal site and the browser.

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.