Check if ajax in the preprocess_node There seems to be a problem when I check if the node is served by ajax or not, I use this function : 

function mytheme_preprocess_node(&amp;$variables) {
    if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) &amp;&amp; strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
        $variables['is_ajax'] = true;
    }else{
        $variables['is_ajax'] = false;
    }
}


The same code works great with preprocess_page or preprocess_html

Is there a way to make it work? or eventually get the page/html variable in the node preprocess ?

Thank you !
The node is cached and this function is only called for the first time the node is displayed for the given view mode.

If you really need to vary your output between ajax and non-ajax context, you need to add a cache context for that. See https://www.drupal.org/docs/8/api/cache-api/cache-contexts and the other related documentation pages. You could use the header:NAME-OF-THE-header cache context I guess.

As an alternative, do whatever needs to be done differently with ajax vs not  with Javascript/CSS