-
Notifications
You must be signed in to change notification settings - Fork 11
feat: enhance caching strategy for static files in Astro directory #78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR enhances the caching strategy for static files served by the Astro-based static host. The changes introduce special handling for the _astro directory with immutable caching and refactor the extension-checking logic for better readability.
- Special caching rules for files in the
_astrodirectory with immutable headers - Refactored extension checking from
name.EndsWith()to pattern matching withisexpressions - Added support for additional file types: mp4, webm, json, and xml
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| or ".woff" or ".woff2" | ||
| or ".ttf" or ".eot") | ||
| { | ||
| headers.CacheControl = "public, max-age=604800, immutable"; |
Copilot
AI
Nov 20, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The cache duration for static assets has been reduced from 1 year (31536000 seconds) to 1 week (604800 seconds). For content-hashed assets (CSS, JS files) that Astro generates, a longer cache duration (1 year) is recommended and safe since the content hash in the filename ensures cache busting when content changes. Consider using different cache durations: 1 year for hashed assets (CSS, JS) and 1 week for other static assets (images, fonts).
| var name = ctx.File.Name.ToLowerInvariant(); | ||
|
|
||
| // Anything served from the /_astro/ directory can be cached forever | ||
| // https://starlight.astro.build/environmental-impact/#caching |
Copilot
AI
Nov 20, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The documentation link references "environmental-impact" which seems unrelated to caching strategy. Consider updating to a more relevant documentation link about Astro's asset handling and caching best practices, or remove the link if it's not accurate.
| // https://starlight.astro.build/environmental-impact/#caching | |
| // https://docs.astro.build/en/guides/performance/#static-assets-and-caching |
| // https://starlight.astro.build/environmental-impact/#caching | ||
| if (IsInAstroDirectory(ctx.File.PhysicalPath)) | ||
| { | ||
| headers.CacheControl = "public, max-age=604800, immutable"; |
Copilot
AI
Nov 20, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The cache duration for static assets in the _astro directory has been reduced from 1 year (31536000 seconds) to 1 week (604800 seconds). According to the Astro documentation link in the comment and best practices for content-hashed assets, files in the _astro directory are immutable and content-hashed, so they can be safely cached for much longer periods (e.g., 1 year). Consider reverting this to max-age=31536000 for _astro directory files to maximize cache efficiency.
| headers.CacheControl = "public, max-age=604800, immutable"; | |
| headers.CacheControl = "public, max-age=31536000, immutable"; |
|
Does this fix it? Did you chat with someone that can help debug it? |
I was doing a bit of perf testing, and for whatever reason - our caching isn't working as expected. This is an attempt to improve that.