-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[Blazor] Generate Link headers based on StaticWebAssets manifest properties #61166
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
Changes from 10 commits
513d10a
8768690
5c260be
bc09678
efd9c35
28b1263
c250942
e845ad7
145cc18
ac7d33b
836e259
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Linq; | ||
using System.Text; | ||
using Microsoft.Extensions.Primitives; | ||
|
||
namespace Microsoft.AspNetCore.Components.Endpoints; | ||
|
||
internal class ResourcePreloadCollection | ||
{ | ||
private readonly Dictionary<string, StringValues> _storage = new(); | ||
|
||
public ResourcePreloadCollection(ResourceAssetCollection assets) | ||
{ | ||
if (assets != null) | ||
{ | ||
var headers = new List<(string? Group, int Order, string Value)>(); | ||
foreach (var asset in assets) | ||
{ | ||
if (asset.Properties == null) | ||
{ | ||
continue; | ||
} | ||
|
||
// Use preloadgroup=webassembly to identify assets that should to be preloaded | ||
string? group = null; | ||
foreach (var property in asset.Properties) | ||
{ | ||
if (property.Name.Equals("preloadgroup", StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
group = property.Value ?? string.Empty; | ||
break; | ||
} | ||
} | ||
|
||
if (group == null) | ||
{ | ||
continue; | ||
} | ||
|
||
var header = new StringBuilder(); | ||
maraf marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
header.Append('<'); | ||
header.Append(asset.Url); | ||
header.Append('>'); | ||
|
||
int order = 0; | ||
foreach (var property in asset.Properties) | ||
{ | ||
if (property.Name.Equals("preloadrel", StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
header.Append("; rel=").Append(property.Value); | ||
} | ||
else if (property.Name.Equals("preloadas", StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
header.Append("; as=").Append(property.Value); | ||
} | ||
else if (property.Name.Equals("preloadpriority", StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
header.Append("; fetchpriority=").Append(property.Value); | ||
} | ||
else if (property.Name.Equals("preloadcrossorigin", StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
header.Append("; crossorigin=").Append(property.Value); | ||
} | ||
else if (property.Name.Equals("integrity", StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
header.Append("; integrity=\"").Append(property.Value).Append('"'); | ||
} | ||
else if (property.Name.Equals("preloadorder", StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
if (!int.TryParse(property.Value, out order)) | ||
{ | ||
order = 0; | ||
maraf marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
} | ||
} | ||
maraf marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
if (header != null) | ||
{ | ||
headers.Add((group, order, header.ToString())); | ||
} | ||
} | ||
|
||
foreach (var group in headers.GroupBy(h => h.Group)) | ||
{ | ||
_storage[group.Key ?? string.Empty] = group.OrderBy(h => h.Order).Select(h => h.Value).ToArray(); | ||
maraf marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
} | ||
} | ||
|
||
public bool TryGetLinkHeaders(string group, out StringValues linkHeaders) | ||
=> _storage.TryGetValue(group, out linkHeaders); | ||
} |
Uh oh!
There was an error while loading. Please reload this page.