Closed javiercn closed 2 days ago
Thank you for submitting this for API review. This will be reviewed by @dotnet/aspnet-api-review at the next meeting of the ASP.NET Core API Review group. Please ensure you take a look at the API review process documentation and ensure that:
[API Review]
RazorComponentsEndpointConventionBuilderExtensions.WithResourceCollection
should require non-nullable manifestPath
ResourceAsset.Url
should be ResourceAsset.Path
(to match our usual pattern)
Current status is that we need to figure out a good name for the main public API. We can't review the other pieces properly until we understand that.
[API Review]
Blazor discussed and proposes "PopulateResourceCollectionFromStaticAssets"
Maybe "AssetResourceCollection" would be clearer than "ResourceAssetCollection"
MapStaticWebAssets maps endpoints; MapRazorPages maps pages; PopulateResourceCollectionFromStaticAssets connects the two
Why isn't PopulateResourceCollectionFromStaticAssets part of MapRazorPages (for the default manifest path)?
If you try to use @Assets["somefile.js"]
without having called MapStaticAssets, it will just be the identity function
We don't want to call the component property "Resources" because that would probably collide with a resx file
We remain concerned about a case where you pull in the wrong asset-related type via completion and auto-imports and then you end up with a bunch of name conflicts
The two names users will actually see an use are of the extension method and the component property
WithStaticAssets seems preferable to WithAssets for consistency with MapStaticAssets
An asset has a path; a resource has a URL
ResourceAssetCollection seems more important than StaticAssetDescriptor, so it should get the better name
"ResourceAsset" => "StaticAssetResource"
Proposal [rejected]:
In the future, if other varieties of assets are added to the AssetResourceCollection, they'll get their own extension method, rather than expanding the functionality of WithStaticAssets
Accepted name changes:
Description
These set of APIs allow Blazor, MVC and Razor pages to map well-known URLs to content-specific (fingerprinted URLs).
Scenarios
Startup/Program APIs
The list of assets is defined on a per-endpoint basis. MVC and RazorPages opt-in to the feature via a call to
WithResourceCollection
following the call toMapRazorPages()
orMapControllers()
.This call adds a
ResourceAssetCollection
to the Metadata collection of the endpoints, which contains the mapping between human-readable URLs and content-specific URLs.From there, other components can retrieve the mapping from endpoint metadata.
Since we can have calls to map different manifests,
WithResourceCollection
takes a parameter to provide the Id for the matchingMatchStaticAssets
.Consuming the mapped assets
The assets are consumed indirectly from MVC and Razor Pages via existing
Script
,Image
,Link
, andUrl
tag helpers.In Blazor, the assets are consumed via the
Assets
property inComponentBase
which exposes an indexer to resolve the fingerprinted url for a given asset.This in the future will be cleaner with a compiler feature similar to the Url tag helper in MVC that transforms "~/app.css" into the above code inside the href attribute.
In addition to this, there are two built-in components to generate an importmap for scripts. See here for details.
TL;DR: Creates a mapping for calls to
import
and scripttype="module"
that optionally includes integrity information. A sample:There is an ImportMap blazor component
and in MVC we extended the script tag helper to support it
Layering
I included this section to explain the reasons for some of the APIs below.
Two important things:
With this in mind, information for the assets is exposed differently from StaticAssets and Components
The Component endpoints assembly is the one responsible for mapping the
StaticAssetDescriptor
s into theResourceAssetCollection
so that Blazor and MVC can consume them.Microsoft.AspNetCore.Components.dll
Microsoft.AspNetCore.Components.Endpoints
Assembly Microsoft.AspNetCore.WebUtilities
Assembly Microsoft.AspNetCore.Mvc.RazorPages
Microsoft.AspNetCore.Mvc.ViewFeatures
Assembly Microsoft.AspNetCore.Mvc.TagHelpers.dll
Assembly Microsoft.AspNetCore.StaticAssets.dll