From ca252dc30ff259ec5267e00605040fbf445b107d Mon Sep 17 00:00:00 2001 From: "Artemov Ivan (ZOXEXIVO)" Date: Sat, 11 Feb 2023 01:54:00 +0300 Subject: [PATCH] Remove prerendering --- .../Prerender/Extensions/BotExtensions.cs | 19 ----- .../Prerender/PrerenderMiddeware.cs | 69 ------------------- src/Backend/Geen.Web/Startup.cs | 5 +- 3 files changed, 1 insertion(+), 92 deletions(-) delete mode 100644 src/Backend/Geen.Web/Application/Prerender/Extensions/BotExtensions.cs delete mode 100644 src/Backend/Geen.Web/Application/Prerender/PrerenderMiddeware.cs diff --git a/src/Backend/Geen.Web/Application/Prerender/Extensions/BotExtensions.cs b/src/Backend/Geen.Web/Application/Prerender/Extensions/BotExtensions.cs deleted file mode 100644 index 611c8f9..0000000 --- a/src/Backend/Geen.Web/Application/Prerender/Extensions/BotExtensions.cs +++ /dev/null @@ -1,19 +0,0 @@ -namespace Geen.Web.Application.Prerender.Extensions -{ - public static class BotExtensions - { - public static bool IsGoogleBot(this string userAgent) - { - userAgent = userAgent?.ToLower() ?? ""; - - return userAgent.Contains("google"); - } - - public static bool IsYandexBot(this string userAgent) - { - userAgent = userAgent?.ToLower() ?? ""; - - return userAgent.Contains("yandex"); - } - } -} diff --git a/src/Backend/Geen.Web/Application/Prerender/PrerenderMiddeware.cs b/src/Backend/Geen.Web/Application/Prerender/PrerenderMiddeware.cs deleted file mode 100644 index f47d35a..0000000 --- a/src/Backend/Geen.Web/Application/Prerender/PrerenderMiddeware.cs +++ /dev/null @@ -1,69 +0,0 @@ -using System.IO; -using Geen.Data.Settings; -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Http.Extensions; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Options; - -namespace Geen.Web.Application.Prerender -{ - public static class PrerenderMiddeware - { - private const string PrerenderUrlParam = "?_escaped_fragment_="; - - public static void UsePrerenderedHtmls(this IApplicationBuilder app) - { - app.Use(async (context, func) => - { - var displayUrl = context.Request.GetEncodedPathAndQuery(); - - if (displayUrl.StartsWith("/api") || displayUrl.StartsWith("/metrics")) - { - await func(); - return; - } - - var userAgent = context.Request.Headers["User-Agent"].ToString(); - - if (displayUrl.EndsWith(PrerenderUrlParam)) - { - var staticPath = context.RequestServices - .GetRequiredService>() - .Value.Prerender.StaticPath; - - var urlPath = context.Request.Path.ToString(); - - if (urlPath.EndsWith("/")) - urlPath = urlPath.Remove(urlPath.Length - 1); - - if (urlPath.Length == 0) - urlPath = "/index"; - - var staticFilePath = staticPath + urlPath + ".html"; - - if (!File.Exists(staticFilePath)) - { - var logger = context.RequestServices.GetService>(); - - logger.LogError("Path: {Path} does not exist", staticFilePath); - - context.Response.StatusCode = 404; - return; - } - - await using var stream = new FileStream(staticFilePath, FileMode.Open, FileAccess.Read); - - context.Response.ContentType = "text/html"; - - await stream.CopyToAsync(context.Response.Body); - - return; - } - - await func(); - }); - } - - } -} diff --git a/src/Backend/Geen.Web/Startup.cs b/src/Backend/Geen.Web/Startup.cs index 9c72cf5..dba8256 100644 --- a/src/Backend/Geen.Web/Startup.cs +++ b/src/Backend/Geen.Web/Startup.cs @@ -1,7 +1,6 @@ using System; using System.IO.Compression; using Geen.Web.Application; -using Geen.Web.Application.Prerender; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.ResponseCompression; using Microsoft.Extensions.Configuration; @@ -75,9 +74,7 @@ public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory) app.UseResponseCompression(); app.UseFallbackRedirects(); - - app.UsePrerenderedHtmls(); - + app.UseStaticFiles(new StaticFileOptions { OnPrepareResponse = context =>