
Caching is crucial for improving website performance, but sometimes it can cause problems, especially when users see outdated content. For static sites like those built with Gatsby.js, proper cache management is essential to ensure users always receive the latest content without requiring a hard refresh (Ctrl + F5
).
In this blog post, we’ll walk through how to configure Nginx to add headers that solve caching issues. This guide applies not only to Gatsby.js sites but also to other static and dynamic sites.
When a browser requests resources from your server, it relies on caching headers to decide whether to fetch a new version or use a cached one. The wrong caching headers can cause the browser to use stale files, leading to:
Adding the right headers in Nginx ensures that:
The Cache-Control
header tells browsers how long they can cache a resource. For Gatsby.js and similar frameworks, use:
app-abc123.js
(which include content hashes in filenames) can be cached for a long time because the filename changes when the content changes.Here’s an example Nginx configuration file to manage caching effectively:
server {listen 80;server_name yourdomain.com;# Root directory for your Gatsby siteroot /path/to/your/site/public;index index.html;# HTML files - no cachinglocation / {add_header Cache-Control "no-cache, no-store, must-revalidate";add_header Pragma "no-cache";add_header Expires 0;try_files $uri /index.html;}# Static assets - cache for a yearlocation ~* \.(?:ico|css|js|gif|jpe?g|png|woff2?|eot|ttf|svg|otf)$ {expires 1y;access_log off;add_header Cache-Control "public, max-age=31536000, immutable";}}
HTML Files:
Cache-Control: no-cache, no-store, must-revalidate
: Ensures browsers always fetch fresh HTML.Pragma: no-cache
and Expires 0
: Compatibility with older browsers.Static Assets (CSS, JS, Images, Fonts):
Cache-Control: public, max-age=31536000, immutable
: Allows caching for one year. The immutable
directive tells browsers the file won’t change, reducing unnecessary network requests.try_files: Ensures that if the requested file doesn’t exist, Nginx serves index.html
(important for single-page applications like Gatsby).
Locate the Nginx configuration file for your site, typically found in:
/etc/nginx/sites-available/yourdomain
/etc/nginx/conf.d/yourdomain.conf
Open the file using a text editor:
sudo nano /etc/nginx/sites-available/yourdomain
Paste the above configuration inside the server
block for your site.
Before restarting Nginx, ensure there are no syntax errors:
sudo nginx -t
Apply the changes by reloading Nginx:
sudo systemctl reload nginx
styles-abc123.css
). This ensures browsers fetch a new file when the content changes, avoiding stale assets.F12
to open Developer Tools./index.html
or app-abc123.js
).For HTML:
Cache-Control: no-cache, no-store, must-revalidatePragma: no-cacheExpires: 0
For Static Assets:
Cache-Control: public, max-age=31536000, immutable
Run the following command to inspect the headers:
curl -I http://yourdomain.com
Example output for HTML files:
HTTP/1.1 200 OKCache-Control: no-cache, no-store, must-revalidatePragma: no-cacheExpires: 0
Proper caching headers in Nginx are critical for solving cache issues in Gatsby.js and other sites. By following this guide, you can ensure that:
This approach balances performance and freshness, ensuring an excellent user experience.
Your insights drive us! For any questions, feedback, or thoughts, feel free to connect:
If you found this guide beneficial, don’t hesitate to share it with your network. Until the next guide, happy coding! 🚀
Quick Links
Legal Stuff