Implement enhanced static file caching and improve calendar event loading with detailed logging
This commit introduces a new static file caching strategy in Program.cs, optimizing cache headers for Blazor assets to improve performance and ensure fresh content after deployments. Additionally, the Calendar component in Index.razor has been updated to include comprehensive logging for event loading, handling null occurrences, and error management during calendar item creation. The CalendarEventItem model is also initialized to prevent null reference issues. These changes enhance the application's reliability and user experience.
This commit is contained in:
+26
-1
@@ -1,5 +1,6 @@
|
||||
using Data;
|
||||
using Microsoft.AspNetCore.DataProtection;
|
||||
using Microsoft.AspNetCore.StaticFiles;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using MudBlazor.Services;
|
||||
using Serilog;
|
||||
@@ -268,7 +269,31 @@ app.UseRouting();
|
||||
app.UseAuthentication();
|
||||
app.UseAuthorization();
|
||||
|
||||
app.UseStaticFiles();
|
||||
// Configure static files with proper cache headers for Blazor assets
|
||||
app.UseStaticFiles(new StaticFileOptions
|
||||
{
|
||||
OnPrepareResponse = ctx =>
|
||||
{
|
||||
// Blazor framework files: Use ETags with short cache and revalidation
|
||||
// This allows caching for performance but ensures fresh files after deployments
|
||||
// Browser will check ETag on each request (304 Not Modified if unchanged)
|
||||
if (ctx.File.Name.Contains("_framework") ||
|
||||
ctx.File.Name.Contains("blazor") ||
|
||||
ctx.File.Name.EndsWith(".dll") ||
|
||||
ctx.File.Name.EndsWith(".wasm"))
|
||||
{
|
||||
// Cache for 12 hours, but must revalidate (check ETag) before using
|
||||
// If file hasn't changed, browser gets 304 Not Modified (no download)
|
||||
// If file changed, browser downloads new version
|
||||
ctx.Context.Response.Headers.Append("Cache-Control", "public, max-age=43200, must-revalidate");
|
||||
}
|
||||
else
|
||||
{
|
||||
// Other static files (CSS, images, etc.) can be cached long-term
|
||||
ctx.Context.Response.Headers.Append("Cache-Control", "public, max-age=31536000");
|
||||
}
|
||||
}
|
||||
});
|
||||
app.UseAntiforgery();
|
||||
|
||||
app.MapRazorComponents<App>()
|
||||
|
||||
Reference in New Issue
Block a user