Skip to content

Commit ab2ef9d

Browse files
committed
add CDN cache-control headers to status card image
1 parent 7991704 commit ab2ef9d

5 files changed

Lines changed: 70 additions & 2 deletions

File tree

StatusPageSharp.slnx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@
99
<Folder Name="/tests/">
1010
<Project Path="tests/StatusPageSharp.Domain.Tests/StatusPageSharp.Domain.Tests.csproj" />
1111
<Project Path="tests/StatusPageSharp.Infrastructure.Tests/StatusPageSharp.Infrastructure.Tests.csproj" />
12+
<Project Path="tests/StatusPageSharp.Web.Tests/StatusPageSharp.Web.Tests.csproj" />
1213
</Folder>
1314
</Solution>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using Microsoft.AspNetCore.Http;
2+
using Microsoft.Net.Http.Headers;
3+
4+
namespace StatusPageSharp.Web.Caching;
5+
6+
public static class StatusCardResponseCacheHeaders
7+
{
8+
private const string CdnCacheControlHeaderName = "CDN-Cache-Control";
9+
private const string CloudflareCdnCacheControlHeaderName = "Cloudflare-CDN-Cache-Control";
10+
private const string NoStoreDirective = "no-store";
11+
12+
public static void Apply(IHeaderDictionary headers)
13+
{
14+
headers[HeaderNames.CacheControl] = NoStoreDirective;
15+
headers[CdnCacheControlHeaderName] = NoStoreDirective;
16+
headers[CloudflareCdnCacheControlHeaderName] = NoStoreDirective;
17+
}
18+
}

src/StatusPageSharp.Web/Extensions/ApiEndpointRouteBuilderExtensions.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using StatusPageSharp.Application.Abstractions;
2+
using StatusPageSharp.Web.Caching;
23
using StatusPageSharp.Web.Rendering;
34

45
namespace StatusPageSharp.Web.Extensions;
@@ -59,15 +60,19 @@ CancellationToken cancellationToken
5960
.MapGet(
6061
"/status-card.png",
6162
async (
63+
HttpContext httpContext,
6264
IPublicStatusService publicStatusService,
6365
CancellationToken cancellationToken
6466
) =>
65-
TypedResults.File(
67+
{
68+
StatusCardResponseCacheHeaders.Apply(httpContext.Response.Headers);
69+
return TypedResults.File(
6670
SocialStatusCardRenderer.Render(
6771
await publicStatusService.GetSiteSummaryAsync(cancellationToken)
6872
),
6973
"image/png"
70-
)
74+
);
75+
}
7176
)
7277
.CacheOutput("StatusCard");
7378

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Microsoft.AspNetCore.Http;
2+
using Microsoft.Net.Http.Headers;
3+
using StatusPageSharp.Web.Caching;
4+
5+
namespace StatusPageSharp.Web.Tests.Caching;
6+
7+
public class StatusCardResponseCacheHeadersTests
8+
{
9+
[Fact]
10+
public void Apply_SetsNoStoreHeaders_ForBrowsersAndCdns()
11+
{
12+
var headers = new HeaderDictionary();
13+
14+
StatusCardResponseCacheHeaders.Apply(headers);
15+
16+
Assert.Equal("no-store", headers[HeaderNames.CacheControl].ToString());
17+
Assert.Equal("no-store", headers["CDN-Cache-Control"].ToString());
18+
Assert.Equal("no-store", headers["Cloudflare-CDN-Cache-Control"].ToString());
19+
}
20+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>net10.0</TargetFramework>
4+
<LangVersion>14.0</LangVersion>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
<IsPackable>false</IsPackable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="coverlet.collector" />
12+
<PackageReference Include="Microsoft.NET.Test.Sdk" />
13+
<PackageReference Include="xunit" />
14+
<PackageReference Include="xunit.runner.visualstudio" />
15+
</ItemGroup>
16+
17+
<ItemGroup>
18+
<Using Include="Xunit" />
19+
</ItemGroup>
20+
21+
<ItemGroup>
22+
<ProjectReference Include="..\..\src\StatusPageSharp.Web\StatusPageSharp.Web.csproj" />
23+
</ItemGroup>
24+
</Project>

0 commit comments

Comments
 (0)