I am using hook_node_access
to decide the whether a node should be visible to anonymous users based on their IP address. To prevent this being cached I am using \Drupal::service('page_cache_kill_switch')->trigger()
within hook_node_view_alter
, but if access is forbidden then hook_node_view_alter
is not run and so the cache is not killed.
Where should I put \Drupal::service('page_cache_kill_switch')->trigger()
? Is there a better way to do this?