|
20 | 20 | if (!builder.Environment.IsDevelopment()) |
21 | 21 | { |
22 | 22 | // Bundle and minify CSS files in production only |
23 | | - pipeline.MinifyCssFiles(); |
24 | 23 | pipeline.AddCssBundle("/css/bundle.min.css", |
25 | 24 | "css/site.css", |
26 | 25 | "css/layout.css"); |
27 | 26 |
|
28 | 27 | // Bundle and minify JavaScript files in production only |
29 | | - pipeline.MinifyJsFiles(); |
30 | 28 | pipeline.AddJavaScriptBundle("/js/bundle.min.js", |
31 | 29 | "js/site.js", |
32 | 30 | "js/analytics.js", |
33 | 31 | "js/theme-switcher.js"); |
| 32 | + |
| 33 | + // Enable minification for all CSS files |
| 34 | + pipeline.MinifyCssFiles(); |
| 35 | + |
| 36 | + // Enable minification for all JavaScript files |
| 37 | + pipeline.MinifyJsFiles(); |
| 38 | + } |
| 39 | + else |
| 40 | + { |
| 41 | + // In development, still enable basic minification for testing |
| 42 | + pipeline.MinifyCssFiles("css/*.css"); |
| 43 | + pipeline.MinifyJsFiles("js/*.js"); |
34 | 44 | } |
35 | | - // In development, no bundling or minification - serve files directly |
36 | 45 | }); |
37 | 46 |
|
38 | 47 | // Add services to the container. |
|
94 | 103 |
|
95 | 104 | // Register content service |
96 | 105 | builder.Services.AddScoped<IContentService, ContentService>(); |
97 | | -builder.Services.AddSingleton<IVersionService, VersionService>(); |
98 | 106 |
|
99 | 107 | var app = builder.Build(); |
100 | 108 |
|
|
124 | 132 |
|
125 | 133 | if (!app.Environment.IsDevelopment()) |
126 | 134 | { |
127 | | - app.UseWebOptimizer(); // Only use WebOptimizer in production |
| 135 | + app.UseWebOptimizer(); // Use WebOptimizer in production |
| 136 | + |
| 137 | + // Add middleware to handle cache headers for WebOptimizer files |
| 138 | + app.Use(async (context, next) => |
| 139 | + { |
| 140 | + if (context.Request.Path.StartsWithSegments("/css/bundle.min.css") || |
| 141 | + context.Request.Path.StartsWithSegments("/js/bundle.min.js")) |
| 142 | + { |
| 143 | + // Set headers to ensure proper cache behavior for bundled files |
| 144 | + context.Response.OnStarting(() => |
| 145 | + { |
| 146 | + context.Response.Headers.CacheControl = "public,max-age=31536000,immutable"; |
| 147 | + context.Response.Headers.Vary = "Accept-Encoding"; |
| 148 | + return Task.CompletedTask; |
| 149 | + }); |
| 150 | + } |
| 151 | + await next(); |
| 152 | + }); |
128 | 153 | } |
129 | 154 |
|
130 | 155 | app.UseStaticFiles(new StaticFileOptions |
|
133 | 158 | { |
134 | 159 | if (!app.Environment.IsDevelopment()) |
135 | 160 | { |
136 | | - // Cache static files for 30 days in production |
137 | | - ctx.Context.Response.Headers.CacheControl = "public,max-age=2592000"; |
| 161 | + var path = ctx.Context.Request.Path.Value?.ToLowerInvariant(); |
| 162 | + |
| 163 | + // Different caching strategies based on file type and path |
| 164 | + if (path != null) |
| 165 | + { |
| 166 | + // WebOptimizer bundles and files with version query strings - cache aggressively |
| 167 | + if (path.Contains("bundle.min.") || ctx.Context.Request.Query.ContainsKey("v")) |
| 168 | + { |
| 169 | + ctx.Context.Response.Headers.CacheControl = "public,max-age=31536000,immutable"; // 1 year |
| 170 | + } |
| 171 | + // Regular CSS/JS files - shorter cache with validation |
| 172 | + else if (path.EndsWith(".css") || path.EndsWith(".js")) |
| 173 | + { |
| 174 | + ctx.Context.Response.Headers.CacheControl = "public,max-age=3600,must-revalidate"; // 1 hour |
| 175 | + } |
| 176 | + // Images and fonts - medium cache |
| 177 | + else if (path.EndsWith(".png") || path.EndsWith(".jpg") || path.EndsWith(".jpeg") || |
| 178 | + path.EndsWith(".gif") || path.EndsWith(".svg") || path.EndsWith(".webp") || |
| 179 | + path.EndsWith(".woff") || path.EndsWith(".woff2") || path.EndsWith(".ttf")) |
| 180 | + { |
| 181 | + ctx.Context.Response.Headers.CacheControl = "public,max-age=2592000"; // 30 days |
| 182 | + } |
| 183 | + // Other static files - short cache |
| 184 | + else |
| 185 | + { |
| 186 | + ctx.Context.Response.Headers.CacheControl = "public,max-age=3600"; // 1 hour |
| 187 | + } |
| 188 | + } |
| 189 | + |
138 | 190 | ctx.Context.Response.Headers.Vary = "Accept-Encoding"; |
139 | 191 | } |
140 | 192 | else |
|
0 commit comments