Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace ScriptsMiddleware(#15629) #16876

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions OrchardCore.sln
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OrchardCore.Queries.Core",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OrchardCore.Sms.Azure", "src\OrchardCore.Modules\OrchardCore.Sms.Azure\OrchardCore.Sms.Azure.csproj", "{013C8BBF-6879-4B47-80C9-A466923E45E5}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Endpoints", "Endpoints", "{4B655F8B-66A6-44DC-BB6A-C84937BF5163}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -1636,6 +1638,7 @@ Global
{4BAA08A2-878C-4B96-86BF-5B3DB2B6C2C7} = {F23AC6C2-DE44-4699-999D-3C478EF3D691}
{61B358F2-702C-40AA-9DF7-7121248FE6DE} = {F23AC6C2-DE44-4699-999D-3C478EF3D691}
{013C8BBF-6879-4B47-80C9-A466923E45E5} = {A066395F-6F73-45DC-B5A6-B4E306110DCE}
{4B655F8B-66A6-44DC-BB6A-C84937BF5163} = {A066395F-6F73-45DC-B5A6-B4E306110DCE}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {46A1D25A-78D1-4476-9CBF-25B75E296341}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System.Globalization;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using OrchardCore.ContentManagement;
using OrchardCore.Settings;
using OrchardCore.Facebook.Settings;

namespace OrchardCore.Facebook;
internal static class InitSdkEndpoint
{
public static IEndpointRouteBuilder AddInitSdkEndpoint(this IEndpointRouteBuilder builder)
{
builder.MapGet("orchardcore/facebook/sdk/fbsdk.js", HandleAsync)
.AllowAnonymous()
.DisableAntiforgery();

return builder;
}

[Authorize(AuthenticationSchemes = "Api")]
private static async Task<IResult> HandleAsync(
ContentItem model,
ISiteService siteService,
HttpContext httpContext,
bool draft = false)
{
ArgumentNullException.ThrowIfNull(httpContext);

var script = await getInitSdkScript(siteService);

if (script == null)
{
return Results.NotFound();
}

return Results.Ok(script);
}

private static async Task<string> getInitSdkScript(ISiteService siteService)
{
var settings = await siteService.GetSettingsAsync<FacebookSettings>();
var locale = CultureInfo.CurrentUICulture.Name.Replace('-', '_');

var script = $@"(function(d){{
var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {{ return; }}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = ""https://connect.facebook.net/{locale}/{settings.SdkJs}"";
d.getElementsByTagName('head')[0].appendChild(js);
}} (document));";

return script;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System.Globalization;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using OrchardCore.ContentManagement;
using OrchardCore.Settings;
using OrchardCore.Facebook.Settings;

namespace OrchardCore.Facebook;
internal static class LoadSdkEndpoint
{
public static IEndpointRouteBuilder AddLoadSdkEndpoint(this IEndpointRouteBuilder builder)
{
builder.MapGet("orchardcore/facebook/sdk/fb.js", HandleAsync)
.AllowAnonymous()
.DisableAntiforgery();

return builder;
}

[Authorize(AuthenticationSchemes = "Api")]
private static async Task<IResult> HandleAsync(
ContentItem model,
ISiteService siteService,
HttpContext httpContext,
bool draft = false)
{
ArgumentNullException.ThrowIfNull(httpContext);

var script = await getLoadSdkScript(siteService);

if (script == null)
{
return Results.NotFound();
}

return Results.Ok(script);
}

private static async Task<string> getLoadSdkScript(ISiteService siteService)
{
var settings = await siteService.GetSettingsAsync<FacebookSettings>();
var locale = CultureInfo.CurrentUICulture.Name.Replace('-', '_');

var script = default(string);

if (!string.IsNullOrWhiteSpace(settings?.AppId))
{
var options = $"{{ appId:'{settings.AppId}',version:'{settings.Version}'";
options = string.IsNullOrWhiteSpace(settings.FBInitParams)
? string.Concat(options, "}")
: string.Concat(options, ",", settings.FBInitParams, "}");

script = $"window.fbAsyncInit = function(){{ FB.init({options});}};";
}

return script;
}
}

63 changes: 0 additions & 63 deletions src/OrchardCore.Modules/OrchardCore.Facebook/ScriptsMiddleware.cs

This file was deleted.

3 changes: 2 additions & 1 deletion src/OrchardCore.Modules/OrchardCore.Facebook/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public sealed class Startup : StartupBase
{
public override void Configure(IApplicationBuilder builder, IEndpointRouteBuilder routes, IServiceProvider serviceProvider)
{
builder.UseMiddleware<ScriptsMiddleware>();
routes.AddInitSdkEndpoint();
routes.AddLoadSdkEndpoint();
}

public override void ConfigureServices(IServiceCollection services)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using Fluid;
using System.Text;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Microsoft.Net.Http.Headers;
using OrchardCore.ContentManagement;
using OrchardCore.DisplayManagement.Liquid;
using OrchardCore.Environment.Shell.Configuration;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Caching.Memory;

namespace OrchardCore.Liquid.Endpoints.Api;

public static class GetIntellisenseEndpoint
{

public static IEndpointRouteBuilder AddGetIntellisenseEndpoint(this IEndpointRouteBuilder builder)
{
builder.MapGet("/OrchardCore.Liquid/Scripts/liquid-intellisense.js", HandleAsync)
.AllowAnonymous()
.DisableAntiforgery();

return builder;
}

[Authorize(AuthenticationSchemes = "Api")]
private static Task<IResult> HandleAsync(HttpContext httpContext, IMemoryCache cache)
{
const string cacheKey = "LiquidIntellisenseScript";

var cacheControl = $"public, max-age={TimeSpan.FromDays(30).TotalSeconds}, s-max-age={TimeSpan.FromDays(365.25).TotalSeconds}";
if (cache.TryGetValue(cacheKey, out (byte[] Bytes, string ETag) cachedData))
{
if (httpContext.Request.Headers.TryGetValue(HeaderNames.IfNoneMatch, out var etagHeader) &&
etagHeader.Contains(cachedData.ETag))
{
return Task.FromResult(Results.StatusCode(StatusCodes.Status304NotModified));
}

httpContext.Response.Headers[HeaderNames.CacheControl] = cacheControl;
httpContext.Response.Headers[HeaderNames.ContentType] = "application/javascript";
httpContext.Response.Headers[HeaderNames.ETag] = cachedData.ETag;

return Task.FromResult(Results.Bytes(cachedData.Bytes, "application/javascript"));
}

var shellConfiguration = httpContext.RequestServices.GetRequiredService<IShellConfiguration>();
cacheControl = shellConfiguration.GetValue("StaticFileOptions:CacheControl", cacheControl);

var templateOptions = httpContext.RequestServices.GetRequiredService<IOptions<TemplateOptions>>();
var liquidViewParser = httpContext.RequestServices.GetRequiredService<LiquidViewParser>();

var filters = string.Join(',', templateOptions.Value.Filters.Select(x => $"'{x.Key}'"));
var tags = string.Join(',', liquidViewParser.RegisteredTags.Select(x => $"'{x.Key}'"));

var script = $@"[{filters}].forEach(value=>{{if(!liquidFilters.includes(value)){{ liquidFilters.push(value);}}}});
[{tags}].forEach(value=>{{if(!liquidTags.includes(value)){{ liquidTags.push(value);}}}});";

var etag = Guid.NewGuid().ToString("n");
var bytes = Encoding.UTF8.GetBytes(script);
cache.Set(cacheKey,(bytes,etag));

httpContext.Response.Headers[HeaderNames.CacheControl] = cacheControl;
httpContext.Response.Headers[HeaderNames.ContentType] = "application/javascript";
httpContext.Response.Headers[HeaderNames.ETag] = etag;

return Task.FromResult(Results.Bytes(bytes, "application/javascript"));
}

}
70 changes: 0 additions & 70 deletions src/OrchardCore.Modules/OrchardCore.Liquid/ScriptsMiddleware.cs

This file was deleted.

4 changes: 3 additions & 1 deletion src/OrchardCore.Modules/OrchardCore.Liquid/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,20 @@
using OrchardCore.Liquid.ViewModels;
using OrchardCore.Modules;
using OrchardCore.ResourceManagement;
using OrchardCore.Liquid.Endpoints.Api;

namespace OrchardCore.Liquid;

public sealed class Startup : StartupBase
{
public override void Configure(IApplicationBuilder app, IEndpointRouteBuilder routes, IServiceProvider serviceProvider)
{
app.UseMiddleware<ScriptsMiddleware>();
routes.AddGetIntellisenseEndpoint();
}

public override void ConfigureServices(IServiceCollection services)
{
services.AddMemoryCache();
services.AddScoped<ILiquidTemplateManager, LiquidTemplateManager>();

services.Configure<TemplateOptions>(options =>
Expand Down