Skip to content

Commit efd9c35

Browse files
committed
wip
1 parent bc09678 commit efd9c35

File tree

2 files changed

+88
-63
lines changed

2 files changed

+88
-63
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Linq;
5+
using Microsoft.Extensions.Primitives;
6+
7+
namespace Microsoft.AspNetCore.Components.Endpoints;
8+
9+
internal class ResourcePreloadCollection
10+
{
11+
private readonly Dictionary<string?, StringValues> _storage = new();
12+
13+
public ResourcePreloadCollection(ResourceAssetCollection assets)
14+
{
15+
if (assets != null)
16+
{
17+
var headers = new List<(string? Order, string Value)>();
18+
foreach (var asset in assets)
19+
{
20+
if (asset.Properties == null)
21+
{
22+
continue;
23+
}
24+
25+
// Use preloadgroup=webassembly to identify assets that should to be preloaded
26+
string? header = null;
27+
string? group = null;
28+
foreach (var property in asset.Properties)
29+
{
30+
if (property.Name.Equals("preloadgroup", StringComparison.OrdinalIgnoreCase))
31+
{
32+
group = property.Value;
33+
header = $"<{asset.Url}>";
34+
break;
35+
}
36+
}
37+
38+
if (header == null)
39+
{
40+
continue;
41+
}
42+
43+
string? order = null;
44+
foreach (var property in asset.Properties)
45+
{
46+
if (property.Name.Equals("preloadrel", StringComparison.OrdinalIgnoreCase))
47+
{
48+
header = String.Concat(header, "; rel=", property.Value);
49+
}
50+
else if (property.Name.Equals("preloadas", StringComparison.OrdinalIgnoreCase))
51+
{
52+
header = String.Concat(header, "; as=", property.Value);
53+
}
54+
else if (property.Name.Equals("preloadpriority", StringComparison.OrdinalIgnoreCase))
55+
{
56+
header = String.Concat(header, "; fetchpriority=", property.Value);
57+
}
58+
else if (property.Name.Equals("preloadcrossorigin", StringComparison.OrdinalIgnoreCase))
59+
{
60+
header = String.Concat(header, "; crossorigin=", property.Value);
61+
}
62+
else if (property.Name.Equals("integrity", StringComparison.OrdinalIgnoreCase))
63+
{
64+
header = String.Concat(header, "; integrity=\"", property.Value, "\"");
65+
}
66+
else if (property.Name.Equals("preloadorder", StringComparison.OrdinalIgnoreCase))
67+
{
68+
order = property.Value;
69+
}
70+
}
71+
72+
if (header != null)
73+
{
74+
headers.Add((order, header));
75+
}
76+
}
77+
78+
headers.Sort((a, b) => string.Compare(a.Order, b.Order, StringComparison.InvariantCulture));
79+
}
80+
}
81+
82+
public bool TryGetLinkHeaders(string group, out StringValues linkHeaders)
83+
=> _storage.TryGetValue(group, out linkHeaders);
84+
}

src/Components/Endpoints/src/Rendering/EndpointHtmlRenderer.Streaming.cs

Lines changed: 4 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.Diagnostics;
55
using System.Linq;
6+
using System.Reflection.PortableExecutable;
67
using System.Runtime.InteropServices;
78
using System.Text;
89
using System.Text.Encodings.Web;
@@ -321,70 +322,10 @@ private void WriteComponentHtml(int componentId, TextWriter output, bool allowBo
321322

322323
private void AppendWebAssemblyPreloadHeaders()
323324
{
324-
var assets = _httpContext.GetEndpoint()?.Metadata.GetMetadata<ResourceAssetCollection>();
325-
if (assets != null)
325+
var preloads = _httpContext.GetEndpoint()?.Metadata.GetMetadata<ResourcePreloadCollection>();
326+
if (preloads != null && preloads.TryGetLinkHeaders("webassembly", out var linkHeaders))
326327
{
327-
var headers = new List<(string? Order, string Value)>();
328-
foreach (var asset in assets)
329-
{
330-
if (asset.Properties == null)
331-
{
332-
continue;
333-
}
334-
335-
// Use preloadgroup=webassembly to identify assets that should to be preloaded
336-
string? header = null;
337-
foreach (var property in asset.Properties)
338-
{
339-
if (property.Name.Equals("preloadgroup", StringComparison.OrdinalIgnoreCase) && property.Value.Equals("webassembly", StringComparison.OrdinalIgnoreCase))
340-
{
341-
header = $"<{asset.Url}>";
342-
break;
343-
}
344-
}
345-
346-
if (header == null)
347-
{
348-
continue;
349-
}
350-
351-
string? order = null;
352-
foreach (var property in asset.Properties)
353-
{
354-
if (property.Name.Equals("preloadrel", StringComparison.OrdinalIgnoreCase))
355-
{
356-
header = String.Concat(header, "; rel=", property.Value);
357-
}
358-
else if (property.Name.Equals("preloadas", StringComparison.OrdinalIgnoreCase))
359-
{
360-
header = String.Concat(header, "; as=", property.Value);
361-
}
362-
else if (property.Name.Equals("preloadpriority", StringComparison.OrdinalIgnoreCase))
363-
{
364-
header = String.Concat(header, "; fetchpriority=", property.Value);
365-
}
366-
else if (property.Name.Equals("preloadcrossorigin", StringComparison.OrdinalIgnoreCase))
367-
{
368-
header = String.Concat(header, "; crossorigin=", property.Value);
369-
}
370-
else if (property.Name.Equals("integrity", StringComparison.OrdinalIgnoreCase))
371-
{
372-
header = String.Concat(header, "; integrity=\"", property.Value, "\"");
373-
}
374-
else if (property.Name.Equals("preloadorder", StringComparison.OrdinalIgnoreCase))
375-
{
376-
order = property.Value;
377-
}
378-
}
379-
380-
if (header != null)
381-
{
382-
headers.Add((order, header));
383-
}
384-
}
385-
386-
headers.Sort((a, b) => string.Compare(a.Order, b.Order, StringComparison.InvariantCulture));
387-
_httpContext.Response.Headers.Link = StringValues.Concat(_httpContext.Response.Headers.Link, headers.Select(h => h.Value).ToArray());
328+
_httpContext.Response.Headers.Link = StringValues.Concat(_httpContext.Response.Headers.Link, linkHeaders);
388329
}
389330
}
390331

0 commit comments

Comments
 (0)