Skip to content

Conversation

@IEvangelist
Copy link
Member

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.

Copilot AI review requested due to automatic review settings November 20, 2025 16:40
Copilot finished reviewing on behalf of IEvangelist November 20, 2025 16:41
Copy link
Contributor

Copilot AI left a 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 _astro directory with immutable headers
  • Refactored extension checking from name.EndsWith() to pattern matching with is expressions
  • 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";
Copy link

Copilot AI Nov 20, 2025

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).

Copilot uses AI. Check for mistakes.
var name = ctx.File.Name.ToLowerInvariant();

// Anything served from the /_astro/ directory can be cached forever
// https://starlight.astro.build/environmental-impact/#caching
Copy link

Copilot AI Nov 20, 2025

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.

Suggested change
// https://starlight.astro.build/environmental-impact/#caching
// https://docs.astro.build/en/guides/performance/#static-assets-and-caching

Copilot uses AI. Check for mistakes.
// https://starlight.astro.build/environmental-impact/#caching
if (IsInAstroDirectory(ctx.File.PhysicalPath))
{
headers.CacheControl = "public, max-age=604800, immutable";
Copy link

Copilot AI Nov 20, 2025

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.

Suggested change
headers.CacheControl = "public, max-age=604800, immutable";
headers.CacheControl = "public, max-age=31536000, immutable";

Copilot uses AI. Check for mistakes.
@davidfowl
Copy link
Contributor

Does this fix it? Did you chat with someone that can help debug it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants