SPA index disable cache

Hi!

1. The problem I’m having:

I’m using Caddy to host SPA. Problem is: browsers are caching my index.html file. I’ve disabled caching through the Header directive and a filter on the root url + index.html
But users are accessing my SPA from various urls such as https:/domain.tld/some/thing and won’t always access the route url (https:/domain.tld/).

How can I configure a header based on the file returned (index.html) and not the url accessed ?

3. Caddy version: 2.7.6

4. How I installed and ran Caddy: docker official image

d. My complete Caddy config:


{$SERVER_NAME}

log

@spa expression `!path(
        '/', '/static/*', '/docs*', '/api*', '/login', '/logout', '/external*', '/graphql*', '/bundles*', '/contexts*', '/_profiler*', '/_wdt*',
        '*.json*', '*.html', '*.csv', '*.yml', '*.yaml', '*.xml'
    )
    || path('/favicon.ico', '/manifest.json', '/robots.txt', '/sitemap*')`

route {

   @index {
      path /
      path /index.html
   }

   header @index {
      Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
      Pragma "no-cache"
      Expires 0
   }

    root @spa /srv/app/front
    root * /srv/app/public
    try_files {path} /index.html

    php_fastcgi unix//var/run/php/php-fpm.sock
    encode zstd gzip
    file_server
}

See the third example here Common Caddyfile Patterns — Caddy Documentation (but ignore the explanation, it’s backwards, my mistake which will be fixed shortly). Basically you need to wrap your try_files inside a route and put the header after it so that the condition on /index.html is checked after the rewrite.

2 Likes

Thank you so much! I saw the example but didn’t understand the part “index.html” would be matched after rewriting.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.