Skip to content

[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

Merged
merged 11 commits into from
Apr 3, 2025

Conversation

maraf
Copy link
Member

@maraf maraf commented Mar 26, 2025

Use properties generated on StaticWebAsset endpoints to generate Link headers.
The endpoint selection will be part of WebAssembly SDK.

The StaticWebAsset endpoint properties are following

  • preloadgroup is used to identify assets to preload during WebAssembly component pre-rendering
  • preloadrel is translated to rel attribute and defines relationship (preload or modulepreload are values that will be used for runtime assets)
  • preloadas is translated to as attribute and specifies the type of content being loaded (script or fetch are values that will be used for runtime assets)
  • preloadpriority is translated to fetchpriority attribute, is optional, and specifies the priority
  • preloadcrossorigin is translated to crossorigin attribute (anonymous is the value that will be used for runtime assets)
  • integrity is translated to integrity attribute and specifies the integrity of the asset
  • preloadorder is used to sort headers based on priority
Example headers
Link: <_framework/dotnet.356opavw5k.js>; rel=preload; integrity="sha256-6bkzR9FAbZfpGpeWcvro3Cdv5q7Q3lrAcECLjdbxMSo="; as=script; fetchpriority=high; crossorigin=anonymous
Link: <_framework/dotnet.native.xcldtrkobb.js>; rel=preload; integrity="sha256-YXbaINTFw/gbCm/ArwBij7UxWCce5uzsMTB7SOFGGRk="; as=script; fetchpriority=high; crossorigin=anonymous
Link: <_framework/dotnet.runtime.bx28vfxjlk.js>; rel=preload; integrity="sha256-MVTbSi9B/7ru3wD2oGurO6Kdq3jzzwpgPjXBZtvxa0E="; as=script; fetchpriority=high; crossorigin=anonymous
Link: <_framework/dotnet.native.w3oqpgq65o.wasm>; rel=preload; integrity="sha256-dOqfsmWHK5+n/l/CjWWXK73okrrdc9sosIkU8QIcD8s="; as=fetch; fetchpriority=high; crossorigin=anonymous
Link: <_framework/System.Private.CoreLib.87qgib2l06.wasm>; rel=preload; integrity="sha256-pKbvr+/okd1TAIi+LQWskI1w2zrpPicTVP6Rk2FUfxs="; as=fetch; fetchpriority=low; crossorigin=anonymous
Link: <_framework/System.Runtime.InteropServices.JavaScript.uyv1n2071u.wasm>; rel=preload; integrity="sha256-Ftdgt1xNeQcttseov2QUsvHr9/ckGemptorEmOa16iQ="; as=fetch; fetchpriority=low; crossorigin=anonymous

Contributes #58875

@maraf maraf added the area-blazor Includes: Blazor, Razor Components label Mar 26, 2025
@maraf maraf added this to the 10.0-preview4 milestone Mar 26, 2025
@maraf maraf self-assigned this Mar 26, 2025
@maraf maraf marked this pull request as ready for review March 27, 2025 12:12
@maraf maraf requested a review from a team as a code owner March 27, 2025 12:12
@dotnet dotnet locked and limited conversation to collaborators Mar 27, 2025
@dotnet dotnet unlocked this conversation Mar 27, 2025
@maraf maraf requested a review from a team April 1, 2025 13:05
@maraf maraf requested a review from Copilot April 2, 2025 08:55
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds support for generating Link headers based on StaticWebAssets manifest properties for WebAssembly preloading. Key changes include:

  • Propagation and handling of new preload-related properties in the resource collection resolver.
  • Extension of endpoint rendering logic to append preload headers when using WebAssembly.
  • Introduction of a new ResourcePreloadCollection class for grouping and ordering preload header values.

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/Shared/Components/ResourceCollectionResolver.cs Added new string variables and updated AddResource to include preload attributes.
src/Components/Endpoints/test/EndpointHtmlRendererTest.cs Added tests to verify correct generation and ordering of preload Link headers.
src/Components/Endpoints/src/Rendering/EndpointHtmlRenderer.Streaming.cs Updated rendering logic to append WebAssembly preload headers conditionally.
src/Components/Endpoints/src/Builder/ResourcePreloadCollection.cs Introduced new logic to build and order preload Link headers from resource assets.
src/Components/Endpoints/src/Builder/ResourceCollectionConvention.cs Updated endpoint metadata conventions to include ResourcePreloadCollection.


foreach (var group in headers.GroupBy(h => h.Group))
{
_storage[group.Key ?? string.Empty] = group.OrderBy(h => h.Order).Select(h => h.Value).ToArray();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the order here "override" the fetch priority?

Also, rather than starting with a list, could we start already with a Dictionary<string, List> or something like that?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea is that within the same fetch priority, you can define the order, as throttling may apply

Copy link
Member

@javiercn javiercn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor comments but it looks good

@maraf maraf merged commit 4af9efe into dotnet:main Apr 3, 2025
27 checks passed
@maraf maraf deleted the BlazorPreload branch April 3, 2025 17:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-blazor Includes: Blazor, Razor Components
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants