-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
ScriptsMiddleware.cs
70 lines (57 loc) · 2.87 KB
/
ScriptsMiddleware.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using System.Text;
using Fluid;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Microsoft.Net.Http.Headers;
using OrchardCore.DisplayManagement.Liquid;
using OrchardCore.Environment.Shell.Configuration;
namespace OrchardCore.Liquid;
public class ScriptsMiddleware
{
private readonly RequestDelegate _next;
private byte[] _bytes;
private string _etag;
public ScriptsMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext httpContext)
{
if (httpContext.Request.Path.StartsWithSegments("/OrchardCore.Liquid/Scripts", StringComparison.OrdinalIgnoreCase))
{
if (Path.GetFileName(httpContext.Request.Path.Value) == "liquid-intellisense.js")
{
if (httpContext.Request.Headers.TryGetValue(HeaderNames.IfNoneMatch, out var v))
{
if (v.Contains(_etag))
{
httpContext.Response.StatusCode = StatusCodes.Status304NotModified;
return;
}
}
var cacheControl = $"public, max-age={TimeSpan.FromDays(30).TotalSeconds}, s-max-age={TimeSpan.FromDays(365.25).TotalSeconds}";
if (_bytes == null)
{
var templateOptions = httpContext.RequestServices.GetRequiredService<IOptions<TemplateOptions>>();
var liquidViewParser = httpContext.RequestServices.GetRequiredService<LiquidViewParser>();
var shellConfiguration = httpContext.RequestServices.GetRequiredService<IShellConfiguration>();
cacheControl = shellConfiguration.GetValue("StaticFileOptions:CacheControl", cacheControl);
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);}}}});";
_etag = Guid.NewGuid().ToString("n");
_bytes = Encoding.UTF8.GetBytes(script);
}
httpContext.Response.Headers[HeaderNames.CacheControl] = cacheControl;
httpContext.Response.Headers[HeaderNames.ContentType] = "application/javascript";
httpContext.Response.Headers[HeaderNames.ETag] = _etag;
await httpContext.Response.Body.WriteAsync(_bytes, httpContext.RequestAborted);
return;
}
}
await _next.Invoke(httpContext);
}
}