Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 38 additions & 9 deletions src/statichost/StaticHost/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,24 +51,53 @@
OnPrepareResponse = ctx =>
{
var headers = ctx.Context.Response.Headers;
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.
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.
return;
}

if (ctx.File.IsDirectory)
{
return;
}

var extension = Path.GetExtension(ctx.File.Name).ToLowerInvariant();

// Astro hashes CSS/JS files, but HTML files should not be cached
// They'll always reference the hashed assets
if (name.EndsWith(".html") || name.EndsWith(".htm"))
if (extension is ".html" or ".htm")
{
headers.CacheControl = "no-cache, no-store, must-revalidate";
headers.Pragma = "no-cache";
headers.Expires = "0";
}
else if (name.EndsWith(".css") || name.EndsWith(".js")
|| name.EndsWith(".jpg") || name.EndsWith(".jpeg")
|| name.EndsWith(".png") || name.EndsWith(".gif")
|| name.EndsWith(".svg") || name.EndsWith(".webp")
|| name.EndsWith(".woff") || name.EndsWith(".woff2")
|| name.EndsWith(".ttf") || name.EndsWith(".eot"))
else if (extension is ".css" or ".js"
or ".mp4" or ".webm"
or ".json" or ".xml"
or ".jpg" or ".jpeg"
or ".png" or ".gif"
or ".svg" or ".webp"
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.
}

static bool IsInAstroDirectory(string? path)
{
headers.CacheControl = "public, max-age=31536000, immutable";
if (string.IsNullOrEmpty(path))
{
return false;
}

var directorySegments = path.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar)
.Split(Path.DirectorySeparatorChar, StringSplitOptions.RemoveEmptyEntries);

return directorySegments.Any(s => s.Equals("_astro", StringComparison.OrdinalIgnoreCase));
}
}
});
Expand Down
Loading