From 26edf1d4b274a8435b0b5d5b3b78b7bb986c551d Mon Sep 17 00:00:00 2001 From: Brain Date: Mon, 6 Nov 2023 18:35:05 +0100 Subject: [PATCH] Improve SPA cacheing --- etc/nginx/conf.d/default.conf | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/etc/nginx/conf.d/default.conf b/etc/nginx/conf.d/default.conf index 8b91bec..3261fec 100644 --- a/etc/nginx/conf.d/default.conf +++ b/etc/nginx/conf.d/default.conf @@ -9,10 +9,24 @@ server { #access_log /var/log/nginx/host.access.log main; + root /usr/share/nginx/html; + + # routes without dots serve the index.html without caching location / { - root /usr/share/nginx/html; - index index.html index.htm; - try_files $uri $uri/ /index.html; + add_header Cache-Control "no-cache"; + try_files $uri $uri/index.html /index.html; + } + + # static js and css files that get replaced instead of updated + location ~ \.(js|css) { + add_header Cache-Control "public, max-age=31536000, immutable"; + try_files $uri =404; + } + + # cache other static files for 30 days + location ~ \.(?!html) { + add_header Cache-Control "public, max-age=2592000"; + try_files $uri =404; } #error_page 404 /404.html;