2

I am having trouble with "Remember last selection" on views in D8. If I click a link away from the view and click back it does not remember my last selection. If I then refresh the page it does. This view does use Ajax and I cannot change that. Also I read somewhere that it may be due to caching from the headers.

If so what is the best way to add / change headers on a View response?

1

Looks like you have to subscribe to an event and manipulate the headers from there. I originally found this answer on how to manipulate headers in D8 here.

This will force a request to the server:

This file will go into your mymodule/src/EventSubscriber folder.

namespace Drupal\mymodule\EventSubscriber;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;

class NoCacheSubscriber implements EventSubscriberInterface {

  public function RemoveCacheHeaders(FilterResponseEvent $event) {
    $response = $event->getResponse();
    $response->headers->set('Cache-Control', 'private, must-revalidate, max-age=0, no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
  }

  public static function getSubscribedEvents() {
    $events[KernelEvents::RESPONSE][] = array('RemoveCacheHeaders', -10);
    return $events;
  }
}

Then this will go into the your mymodule folder as mymodule.services.yml

    services:
  remove_cache_options_subscriber:
    class: Drupal\mymodule\EventSubscriber\NoCacheSubscriber
    tags:
      - { name: event_subscriber }
  • 1
    this would completly disable cache browser cache, wouldnt it? – anonym-developer Oct 29 '18 at 9:29
  • For that specific page yes – chadpeppers Oct 30 '18 at 20:36
  • 1
    I think this will disable the cache for the whole site. – anonym-developer Oct 30 '18 at 20:47
  • Oh yes you are correct it does. You could get the current route and determine it by that. – chadpeppers Oct 31 '18 at 19:05

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.