etag all pages
This commit is contained in:
12
src/TerribleDev.Blog.Web/Extensions/ArrayExtensions.cs
Normal file
12
src/TerribleDev.Blog.Web/Extensions/ArrayExtensions.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using System;
|
||||
|
||||
namespace TerribleDev.Blog.Web
|
||||
{
|
||||
public static class ArrayExtensions
|
||||
{
|
||||
public static string ToHexString(this byte[] bytes)
|
||||
{
|
||||
return Convert.ToHexString(bytes);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,8 @@ using System.Collections.Concurrent;
|
||||
using Schema.NET;
|
||||
using System.Text.RegularExpressions;
|
||||
using TerribleDev.Blog.Web.Factories;
|
||||
using System.Text;
|
||||
using System.Security.Cryptography;
|
||||
|
||||
namespace TerribleDev.Blog.Web
|
||||
{
|
||||
@@ -123,7 +125,8 @@ namespace TerribleDev.Blog.Web
|
||||
JsonLDString = ld.ToHtmlEscapedString().Replace("https://schema.org", "https://schema.org/true"),
|
||||
JsonLDBreadcrumb = breadcrumb,
|
||||
JsonLDBreadcrumbString = breadcrumb.ToHtmlEscapedString().Replace("https://schema.org", "https://schema.org/true"),
|
||||
HasCode = hasCode
|
||||
HasCode = hasCode,
|
||||
MarkdownMD5 = MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(markdownText)).ToHexString()
|
||||
};
|
||||
var thumbNailUrl = string.IsNullOrWhiteSpace(postSettings.thumbnailImage) ?
|
||||
postImages?.FirstOrDefault() ?? "https://www.gravatar.com/avatar/333e3cea32cd17ff2007d131df336061?s=640" :
|
||||
@@ -176,7 +179,8 @@ namespace TerribleDev.Blog.Web
|
||||
SummaryPlainShort = (postContentPlain.Length <= 147 ? postContentPlain : postContentPlain.Substring(0, 146)) + "...",
|
||||
JsonLDBreadcrumb = breadcrumb,
|
||||
JsonLDBreadcrumbString = breadcrumb.ToHtmlEscapedString().Replace("https://schema.org", "https://schema.org/true"),
|
||||
HasCode = hasCode
|
||||
HasCode = hasCode,
|
||||
MarkdownMD5 = MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(markdownText)).ToHexString()
|
||||
};
|
||||
return new LandingPage()
|
||||
{
|
||||
|
||||
34
src/TerribleDev.Blog.Web/Filters/ETagFilter.cs
Normal file
34
src/TerribleDev.Blog.Web/Filters/ETagFilter.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
|
||||
namespace TerribleDev.Blog.Web.Filters
|
||||
{
|
||||
public class StaticETag: ActionFilterAttribute
|
||||
{
|
||||
public static string staticEtag = MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(DateTimeOffset.Now.ToUnixTimeMilliseconds().ToString())).ToHexString().Substring(0,8);
|
||||
public static ConcurrentDictionary<string, string> cache = new ConcurrentDictionary<string, string>();
|
||||
public override void OnActionExecuting(ActionExecutingContext context)
|
||||
{
|
||||
var etag = context.HttpContext.Request.Headers["If-None-Match"];
|
||||
if(etag == staticEtag)
|
||||
{
|
||||
context.Result = new StatusCodeResult(304);
|
||||
}
|
||||
else
|
||||
{
|
||||
base.OnActionExecuting(context);
|
||||
}
|
||||
}
|
||||
public override void OnActionExecuted(ActionExecutedContext context)
|
||||
{
|
||||
if(context.HttpContext.Response.StatusCode >= 200 && context.HttpContext.Response.StatusCode < 300 && context.HttpContext.Response.Headers.ETag.Count == 0)
|
||||
{
|
||||
context.HttpContext.Response.Headers.Add("ETag", staticEtag);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -22,5 +22,6 @@ namespace TerribleDev.Blog.Web.Models
|
||||
public string JsonLDString { get; set; }
|
||||
BreadcrumbList JsonLDBreadcrumb { get; set; }
|
||||
string JsonLDBreadcrumbString { get; set; }
|
||||
string MarkdownMD5 { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,5 +20,6 @@ namespace TerribleDev.Blog.Web.Models
|
||||
public BreadcrumbList JsonLDBreadcrumb { get; set; }
|
||||
public string JsonLDBreadcrumbString { get; set; }
|
||||
public bool HasCode { get; set; }
|
||||
public string MarkdownMD5 { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,7 @@ using TerribleDev.Blog.Web.Factories;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using WebMarkupMin.AspNetCore6;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using TerribleDev.Blog.Web.Filters;
|
||||
|
||||
namespace TerribleDev.Blog.Web
|
||||
{
|
||||
@@ -58,7 +59,9 @@ namespace TerribleDev.Blog.Web
|
||||
}
|
||||
return postCache;
|
||||
});
|
||||
var controllerBuilder = services.AddControllersWithViews();
|
||||
var controllerBuilder = services.AddControllersWithViews(a => {
|
||||
a.Filters.Add(new StaticETag());
|
||||
});
|
||||
#if DEBUG
|
||||
if (Env.IsDevelopment())
|
||||
{
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Debug",
|
||||
"System": "Information",
|
||||
"Microsoft": "Information"
|
||||
"Default": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user