rename push header

This commit is contained in:
Tommy Parnell
2022-06-01 21:28:56 -04:00
parent 16572e3e6c
commit 96d06da5e4
8 changed files with 105 additions and 14 deletions

View File

@@ -7,9 +7,12 @@ using Microsoft.AspNetCore.Mvc;
using TerribleDev.Blog.Web.Models;
using System.IO;
using Microsoft.AspNetCore.Html;
using TerribleDev.Blog.Web.Filters;
using Microsoft.Extensions.Logging;
namespace TerribleDev.Blog.Web.Controllers
{
[Http2PushFilter]
public class HomeController : Controller
{
private readonly PostCache postCache;

View File

@@ -2,10 +2,12 @@ using System;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using TerribleDev.Blog.Web.Filters;
using TerribleDev.Blog.Web.Models;
namespace TerribleDev.Blog.Web.Controllers
{
[Http2PushFilter]
public class SearchController : Controller
{
private readonly BlogConfiguration configuration;

View File

@@ -3,10 +3,12 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using TerribleDev.Blog.Web.Filters;
using TerribleDev.Blog.Web.Models;
namespace TerribleDev.Blog.Web.Controllers
{
[Http2PushFilter]
public class TagsController : Controller
{
private readonly PostCache postCache;

View File

@@ -0,0 +1,38 @@
using System;
using System.Text;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Logging;
using TerribleDev.Blog.Web.Taghelpers;
namespace TerribleDev.Blog.Web.Filters
{
public class Http2PushFilter : ActionFilterAttribute
{
public override void OnResultExecuted(ResultExecutedContext context)
{
var logger = context.HttpContext.RequestServices.GetService(typeof(ILogger<Http2PushFilter>)) as ILogger<Http2PushFilter>;
logger.LogDebug("Http2PushFilter.OnActionExecuted");
if(!context.HttpContext.Items.TryGetValue(HttpPush.Key, out var links))
{
logger.LogDebug("Did not find any links to push");
return;
}
var linkData = links as System.Collections.Generic.List<string>;
if(linkData == null || linkData.Count == 0) {
logger.LogDebug("Http2PushFilter.OnActionExecuted: No links");
return;
}
var headerBuilder = new StringBuilder();
for(var i = 0; i < linkData.Count; i++) {
var url = linkData[i];
var resolvedUrl = url.StartsWith("~") ? context.HttpContext.Request.PathBase.ToString() + url.Substring(1) : url;
headerBuilder.Append($"<{resolvedUrl}>; rel=preload; as=style");
if(i < linkData.Count - 1) {
headerBuilder.Append(", ");
}
}
logger.LogDebug("Http2PushFilter.OnActionExecuted: " + headerBuilder.ToString());
context.HttpContext.Response.Headers.Add("Link", headerBuilder.ToString());
}
}
}

View File

@@ -13,6 +13,7 @@ using TerribleDev.Blog.Web.Models;
using TerribleDev.Blog.Web.Factories;
using Microsoft.Extensions.Hosting;
using WebMarkupMin.AspNetCore6;
using Microsoft.Extensions.Logging;
namespace TerribleDev.Blog.Web
{
@@ -43,6 +44,8 @@ namespace TerribleDev.Blog.Web
{
services.AddSingleton(blogConfiguration);
}
// enable logging
services.AddLogging();
services.AddSingleton((i) => {
var posts = new BlogFactory().GetAllPostsAsync(Env.IsDevelopment() ? "https://localhost:5001": "https://blog.terrible.dev").Result;
var postCache = BlogCacheFactory.ProjectPostCache(posts);
@@ -67,8 +70,7 @@ namespace TerribleDev.Blog.Web
a.EnableForHttps = true;
})
.AddMemoryCache()
.AddOutputCaching();
.AddMemoryCache();
services.AddWebMarkupMin(a => {
a.AllowMinificationInDevelopmentEnvironment = true;
a.DisablePoweredByHttpHeaders = true;
@@ -132,7 +134,10 @@ namespace TerribleDev.Blog.Web
// },
UpgradeInsecureRequests = true
});
app.UseOutputCaching();
if(env.IsProduction())
{
app.UseOutputCaching();
}
app.UseWebMarkupMin();
app.UseRouting();
app.UseEndpoints(endpoints =>

View File

@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.Encodings.Web;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Razor.Infrastructure;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.Routing;
using Microsoft.AspNetCore.Mvc.TagHelpers;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Razor.TagHelpers;
namespace TerribleDev.Blog.Web.Taghelpers
{
[HtmlTargetElement("link", Attributes = "rel, href, http-2-push")]
public class HttpPush : LinkTagHelper
{
public bool Http2PushEnabled { get; set; } = true;
public static readonly string Key = "http2push-link";
public HttpPush(IWebHostEnvironment hostingEnvironment, TagHelperMemoryCacheProvider cacheProvider, IFileVersionProvider fileVersionProvider, HtmlEncoder htmlEncoder, JavaScriptEncoder javaScriptEncoder, IUrlHelperFactory urlHelperFactory) : base(hostingEnvironment, cacheProvider, fileVersionProvider, htmlEncoder, javaScriptEncoder, urlHelperFactory)
{
}
public override void Process(TagHelperContext context, TagHelperOutput output)
{
if(!this.Http2PushEnabled)
{
return;
}
var url = base.TryResolveUrl(output.Attributes["href"].Value.ToString(), out string resolvedUrl) ? resolvedUrl : output.Attributes["href"].Value.ToString();
var linkList = ViewContext.HttpContext.Items.TryGetValue(Key, out var links) ? links as List<string> : null;
if(linkList == null)
{
linkList = new List<string>() { url };
ViewContext.HttpContext.Items.Add(HttpPush.Key, linkList);
}
else
{
linkList.Add(url);
}
}
}
}

View File

@@ -29,22 +29,22 @@
else
{
<environment names="Development">
<link asp-append-version="true" rel="stylesheet" href="~/css/site.css" />
<link http-2-push asp-append-version="true" rel="stylesheet" href="~/css/site.css" />
</environment>
<environment names="Production">
<link asp-append-version="true" rel="stylesheet" href="~/css/site.min.css" />
<link http-2-push asp-append-version="true" rel="stylesheet" href="~/css/site.min.css" />
</environment>
<environment names="Development">
<link asp-append-version="true" media="screen and (min-width: 769px)" rel="stylesheet" href="~/css/site.desktop.css" />
<link http-2-push asp-append-version="true" media="screen and (min-width: 769px)" rel="stylesheet" href="~/css/site.desktop.css" />
</environment>
<environment names="Production">
<link asp-append-version="true" media="screen and (min-width: 769px)" rel="stylesheet" href="~/css/site.desktop.min.css" />
<link http-2-push asp-append-version="true" media="screen and (min-width: 769px)" rel="stylesheet" href="~/css/site.desktop.min.css" />
</environment>
<environment names="Development">
<link asp-append-version="true" media="screen and (max-width: 768px)" rel="stylesheet" href="~/css/site.mobile.css" />
<link http-2-push asp-append-version="true" media="screen and (max-width: 768px)" rel="stylesheet" href="~/css/site.mobile.css" />
</environment>
<environment names="Production">
<link asp-append-version="true" media="screen and (max-width: 768px)" rel="stylesheet" href="~/css/site.mobile.min.css" />
<link http-2-push asp-append-version="true" media="screen and (max-width: 768px)" rel="stylesheet" href="~/css/site.mobile.min.css" />
</environment>
<environment names="Development">
<link asp-append-version="true" rel="preload" as="script" href="~/js/swi.js" />

View File

@@ -2,11 +2,6 @@
"Logging": {
"LogLevel": {
"Default": "Warning"
},
"Console": {
"LogLevel": {
"Default": "None"
}
}
},
"AllowedHosts": "*",