Caching and HTTP_X_REQUESTED_WITH

Hi

I have a SPA (single page application) build with Kirby, everything works like a charm, super system getkirby is.

I am doing a conditional within footer.php and header.php

$isAjax = !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';

I use this to exclude content ex. tag and other unnecessary content when the request is an ajax call. This keeps requests light.

If I have caching enabled.
c::set('cache', true);

then it caches the ajax call as a “standard” http request of the whole page. So if I refresh the page I only get a partial page (ex. no tag).

Is there a was to get finer control over what get’s inserted into the cache and what get’s fetched from the cache?

In theory I would like to

  1. if ajax call, then make sure to save the whole page as well
  2. when retrieving a cache item, then check for an ajax parameter (maybe a header?) and retrieve the appropriate cache item.

Hope it makes sense.

Have you tried using a content representation which returns only the part you’ld like (e.g. via JSON) instead of hacking if/elses all over your templates & snippets?

PS: are your headers and footers that heavy to figure out another way of sending me through the wire? How much difference do we talk about here (gzipped)?

1 Like

Sometimes all you need is another pair of eyes, the difference is incredible small, will ignore the checks. Thanks for the feedback. content representation also looks like another amazing feature of kirby.