2016-11-22 2 views
0

쿠키 값 'Drupal_visitor_country'를 동적으로 설정하는 drupal 8 사이트가 있습니다. 그러나이 값은 캐시되고 페이지 새로 고침 중에 제대로 검색 할 수 없습니다.Drupal 8 쿠키 캐시 문제

이 값은 theme_preprocess_menu 함수에서 사용하지만 항상 실제 값 대신 캐시 된 쿠키 값을 반환합니다. 이 상황을 극복 할 수있는 방법이 있습니까?

도움이 될 것입니다.

감사

답변

0

사용 EventSubscriber: $events[KernelEvents::REQUEST][] = ['onRequest'];

/** 
* @file 
* Contains \Drupal\kvantstudio\EventSubscriber\KvantstudioEventSubscriber. 
*/ 

namespace Drupal\kvantstudio\EventSubscriber; 

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

/** 
* Event Subscriber KvantstudioEventSubscriber. 
*/ 
class KvantstudioEventSubscriber implements EventSubscriberInterface { 

    /** 
    * Code that should be triggered on event specified 
    */ 
    public function onRequest(GetResponseEvent $event) { 
    if (!isset($_COOKIE['Drupal_visitor_userHash'])) { 
     $uuid = \Drupal::service('uuid'); 
     user_cookie_save(['userHash' => 'user-' . $uuid->generate()]); 
    } 
    } 

    /** 
    * {@inheritdoc} 
    */ 
    public static function getSubscribedEvents() { 
    $events = [];  
    $events[KernelEvents::REQUEST][] = ['onRequest']; 
    return $events; 
    } 
} 
+0

비슷한 시나리오가 있지만 KernelEvents :: REQUEST 이벤트는 캐시되지 않은 페이지에서만 실행되는 것처럼 보이므로 나에게 도움이되지 않습니다. – Rax

0

나를 위해 Middleware API 트릭을했다. 아래를 참조하십시오.

작동하지 않음 :KernelEvents을 사용해 보았습니다. 페이지 요청시 작동하지 않았습니다. KernelEvents::REQUEST은 자산 요청시 실행되지 않았습니다.

작동하지 않음 :services.yml 남용을 시도했지만 실제로 여기 페이지를 제외 할 수 없음을 알았습니다.

renderer.config: 
    required_cache_contexts: ['languages:language_interface', 'theme', 'user.permissions', ... ] 

작동하지 않았다

: 그럼 난 내 자신의 CacheContext을 만들려고. 그것이 또한 배제를위한 길을 제공하지 않는다는 것을 알아 냈습니다. 기껏해야 특정 노드에 대한 모든로드에 대해 고유 한 캐시를 생성 할 수 있지만 이는 캐시를 중지시킵니다. 또한 How to recognize, discover and create? : Middleware API : CacheContextInterface

작업을했다. Drupal 캐싱 전에 실행됩니다. 베스트 페이지 캐시에서 시작하는 것입니다

미들웨어 처리 중에 Drupal이 완전히 부트 스트랩되지 않을 수 있으며 일부 기능이 없을 수도 있습니다.

Mario Vercellotti에게 올바른 방향으로 안내해 주셔서 감사합니다.