|
8 | 8 | namespace Web.Extensions; |
9 | 9 |
|
10 | 10 | public static class EndpointExtensions |
11 | | -{ |
12 | | - public static void MapSitemapEndpoint(this WebApplication app) |
| 11 | +{ public static void MapSitemapEndpoint(this WebApplication app) |
13 | 12 | { |
14 | 13 | app.MapGet("/sitemap.xml", async (HttpContext context, IContentService contentService) => |
15 | 14 | { |
| 15 | + // Enable output caching for sitemap |
| 16 | + context.Response.GetTypedHeaders().CacheControl = new Microsoft.Net.Http.Headers.CacheControlHeaderValue |
| 17 | + { |
| 18 | + Public = true, |
| 19 | + MaxAge = TimeSpan.FromHours(1) |
| 20 | + }; |
16 | 21 | // Define the XML namespace for the sitemap |
17 | 22 | XNamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9"; |
18 | 23 |
|
@@ -106,13 +111,21 @@ public static void MapSitemapEndpoint(this WebApplication app) |
106 | 111 |
|
107 | 112 | // Return the XML document as a string |
108 | 113 | return sitemap.ToString(); |
109 | | - }); |
110 | | - } |
111 | | - |
112 | | - public static void MapRssFeedEndpoint(this WebApplication app) |
| 114 | + }) |
| 115 | + .CacheOutput(policy => policy |
| 116 | + .Expire(TimeSpan.FromHours(1)) |
| 117 | + .SetVaryByHost(true) |
| 118 | + .Tag("sitemap")); |
| 119 | + } public static void MapRssFeedEndpoint(this WebApplication app) |
113 | 120 | { |
114 | 121 | app.MapGet("/feed.rss", async (HttpContext context, IContentService contentService) => |
115 | | - { // Create the RSS feed XML document |
| 122 | + { |
| 123 | + // Enable output caching for RSS feed |
| 124 | + context.Response.GetTypedHeaders().CacheControl = new Microsoft.Net.Http.Headers.CacheControlHeaderValue |
| 125 | + { |
| 126 | + Public = true, |
| 127 | + MaxAge = TimeSpan.FromMinutes(30) |
| 128 | + };// Create the RSS feed XML document |
116 | 129 | var rss = new XDocument( |
117 | 130 | new XDeclaration("1.0", "utf-8", null), |
118 | 131 | new XProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"/xsl/rss.xsl\""), |
@@ -173,6 +186,10 @@ public static void MapRssFeedEndpoint(this WebApplication app) |
173 | 186 |
|
174 | 187 | // Return the XML document as a string |
175 | 188 | return rss.ToString(); |
176 | | - }); |
| 189 | + }) |
| 190 | + .CacheOutput(policy => policy |
| 191 | + .Expire(TimeSpan.FromMinutes(30)) |
| 192 | + .SetVaryByHost(true) |
| 193 | + .Tag("rss")); |
177 | 194 | } |
178 | 195 | } |
0 commit comments