How can I force a view page not to be cached? I created a few views which I use as JSON endpoints for a client application. However, they are not refreshed along with the content they serve.

What I have tried with no success:


Setting Caching to none (within the View page).
Disabled Views cache, (in Views' advanced settings).
Running drush cache-rebuild as a cronjob every 12h.


I know I can use a workaround like /api/my-endpoint?time=1234, but this is not an option for me, since I have no access to the consuming client's code anymore. Neither I plan to recreate all these endpoints in custom code.

How can I force a JSON view page not to be cached?
This is probably not related to caching inside of views, more likely on page level. You can disable page caching with the route option no_cache. Use a RouteSubscriber to alter the routes of views:

/src/Routing/RouteSubscriber.php

&lt;?php

namespace Drupal\mymodule\Routing;

use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\RouteCollection;

class RouteSubscriber extends RouteSubscriberBase {

  protected function alterRoutes(RouteCollection $collection) {

    // no_cache for a specific view
    if ($route = $collection-&gt;get('view.testview.page_1')) {
      $route-&gt;setOption('no_cache', TRUE);
    }

    // or for all view displays of a specific type
    foreach ($collection-&gt;all() as $route) {
      if ($route-&gt;getOption('_view_display_plugin_id') == 'rest_export') {
        $route-&gt;setOption('no_cache', TRUE);
      }
    }

  }
}


mymodule.services.yml

services:
  mymodule.route_subscriber:
    class: Drupal\mymodule\Routing\RouteSubscriber
    tags:
      - { name: event_subscriber }




This would not be necessary, if the view set the correct cache tags for the content it serves, so that the render cache could be invalidated if the content is changed.