Skip to content

Commit 02131b6

Browse files
committed
refactor: remove IVersionService and VersionService, update layout to use asp-append-version for CSS and JS
1 parent fcdd471 commit 02131b6

4 files changed

Lines changed: 61 additions & 42 deletions

File tree

Web/Pages/Shared/_Layout.cshtml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
@inject IConfiguration Configuration
2-
@inject Web.Services.IVersionService VersionService
32
<!DOCTYPE html>
43
<html lang="en">
54
<head>
@@ -92,7 +91,7 @@
9291
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.css" />
9392
<link id="prism-theme" rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/themes/prism.min.css" /> <!-- Site CSS -->
9493
<environment exclude="Development">
95-
<link rel="stylesheet" href="~/css/bundle.min.css[email protected]()" />
94+
<link rel="stylesheet" href="~/css/bundle.min.css" asp-append-version="true" />
9695
</environment>
9796
<environment include="Development">
9897
<link rel="stylesheet" href="~/css/site.css" asp-append-version="true" />
@@ -191,7 +190,7 @@
191190
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/components/prism-bash.min.js"></script>
192191
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/components/prism-yaml.min.js"></script> <!-- Site scripts -->
193192
<environment exclude="Development">
194-
<script src="~/js/bundle.min.js[email protected]()"></script>
193+
<script src="~/js/bundle.min.js" asp-append-version="true"></script>
195194
</environment>
196195
<environment include="Development">
197196
<script src="~/js/site.js" asp-append-version="true"></script>

Web/Program.cs

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,28 @@
2020
if (!builder.Environment.IsDevelopment())
2121
{
2222
// Bundle and minify CSS files in production only
23-
pipeline.MinifyCssFiles();
2423
pipeline.AddCssBundle("/css/bundle.min.css",
2524
"css/site.css",
2625
"css/layout.css");
2726

2827
// Bundle and minify JavaScript files in production only
29-
pipeline.MinifyJsFiles();
3028
pipeline.AddJavaScriptBundle("/js/bundle.min.js",
3129
"js/site.js",
3230
"js/analytics.js",
3331
"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");
3444
}
35-
// In development, no bundling or minification - serve files directly
3645
});
3746

3847
// Add services to the container.
@@ -94,7 +103,6 @@
94103

95104
// Register content service
96105
builder.Services.AddScoped<IContentService, ContentService>();
97-
builder.Services.AddSingleton<IVersionService, VersionService>();
98106

99107
var app = builder.Build();
100108

@@ -124,7 +132,24 @@
124132

125133
if (!app.Environment.IsDevelopment())
126134
{
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+
});
128153
}
129154

130155
app.UseStaticFiles(new StaticFileOptions
@@ -133,8 +158,35 @@
133158
{
134159
if (!app.Environment.IsDevelopment())
135160
{
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+
138190
ctx.Context.Response.Headers.Vary = "Accept-Encoding";
139191
}
140192
else

Web/Services/IVersionService.cs

Lines changed: 0 additions & 6 deletions
This file was deleted.

Web/Services/VersionService.cs

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)