our website is SPA, so only first request is loading the whole page with css/js stuff. every next load is an ajax request. the initial request caches the whole site as expected. code is:


{layout:set name="meta"}
    {exp:social_tags description="Start | Energy"}

<!-- load data for home -->
{stash:embed:ech:stash_featured stash:rubricid="0" stash:limit="7"}
{stash:embed:ech:stash_entries stash:rubricid="0" stash:hideFeatured="7"}
{stash:embed:ech:stash_disrupters stash:rubricid="26"}
{stash:embed:ech:stash_teasers stash:rubricid="41"}

{exp:stash:parse process="end"}

<!-- featured home section -->
<!-- all other entries on home -->
<!-- all other entries on home -->
<!-- all other entries on home -->


{exp:stash:static name="home" refresh="15"}

I find the whole site cached under stashCache/1/index.html Navigating from domain.com to domain.com/site2 and then back to domain.com will overwrite my index.html cache but with missing elements, because we cut them out while loading with ajax.


{if !is_ajax_request}
<!DOCTYPE html>
<html lang="en">
... // thing not to load with every request

loading the landingpage over a XMLHttpRequest will not break the site, but will overwrite the cache for index.php. if I really reload then with F5, the site has no styles, because the cached template has no

any suggestions for static caching and SPA sites?


There is a mod_rewrite condition in the .htaccess file that excludes ajax requests from being rewritten to the cache file. You could choose to comment this out so that your ajax requests are cached. Note that your ajax requests should be to unique urls:

# Exclude AJAX
# RewriteCond %{HTTP:X-Requested-With} !=XMLHttpRequest

(see: https://github.com/croxton/Stash/wiki/Static-cache-setup)

Alternatively, you could exclude ajax requests from caching:

{if !is_ajax_request}
  • yes, but the default configuration of your .htaccess the line is not commented out. RewriteCond %{HTTP:X-Requested-With} !=XMLHttpRequest and this should not overwrite stashCache/1/index.html when accessing my landingpage over ajax. or am I wrong? – Thomas Hartmann Sep 18 '16 at 17:35
  • The htaccess determines whether to rewrite to the cache should that cache file already exist. It doesn't determine whether something should be cached in the first place. A cache file corresponds to a unique url on your site, so therefore if you want to cache ajax requests those requests should be to a unique url. In practice that means if you want to static cache a page you can't use query strings or POST to pass parameters to the page, you must use url segments. Query string db caching IS possible with Stash if option is set in your config, but you can only use that with {exp:stash:cache} – Mark Croxton Sep 28 '16 at 9:38
  • I should add that I do NOT recommend enabling query string caching unless you very carefully validate your parameters, since it provides a point of entry for an attacker to bloat your cache with invalid parameters. – Mark Croxton Sep 28 '16 at 9:40

Your Answer

