Check if ajax in the preprocess_node
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