Skip to content
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

[Blazor] Adds compresion support for all assets in an application #55558

Merged
merged 13 commits into from May 13, 2024

Conversation

javiercn
Copy link
Member

@javiercn javiercn commented May 6, 2024

The PR adds a new MapStaticAssetEndpoints routerware that reads a manifest generated at build / publish time and maps the endpoints defined in the manifest to the files in the application wwwroot folder.

This PR adds a MatcherPolicy to perform content negotiation based on the Accept-Encoding header. Compressed resources contain a ContentEncoding attribute in their metadata that is used to filter out the compressed asset to serve to the client based on the Accept-Encoding header.

The content encoding has an associated Quality value that represents the 'server preference' for the encoding. We always use the client preference and only rely on the server preference to break ties.

This process is completely driven by de the build and transparent to the runtime, if in the future we add support for zstd, sbr, etc. we don't need to change the runtime.

The logic for serving static files is borrowed from the static files middleware, with simplifications and additions. I've ported the relevant tests to ensure a high degree of compatibility.

The logic is new "routerware" instead of directly baked into the static files middleware because we do not want to pollute that middleware with more complex logic and we are going to be layering fingerprinting on top of this change, which will register more endpoints that will include more custom headers, which is not suitable for the static files middleware.

During development we wrap the endpoints we generate to support changing the files while the app is running (we recompute some of the values on the fly and also register a fallback route that matches file patterns to serve new files added while the app is running).

The build/publish process computes all the required information about the assets that is used to emit the response. The ETag and Last-Modified values are computed using the Base64(SHA256) hash of the content and the LastWrite on the file at build/publish time.

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-blazor Includes: Blazor, Razor Components label May 6, 2024
@javiercn javiercn force-pushed the javiercn/add-static-web-assets-endpoints branch from a549ca3 to 8a5baa6 Compare May 7, 2024 14:25
@javiercn javiercn changed the title [Blazor] Adds static asset endpoints, serve compressed assets [Blazor] Adds compresion support for all assets in an application May 9, 2024
@javiercn javiercn marked this pull request as ready for review May 9, 2024 16:51
@javiercn javiercn force-pushed the javiercn/add-static-web-assets-endpoints branch 2 times, most recently from 96464a8 to f91ac86 Compare May 10, 2024 10:58
Copy link
Member

@MackinnonBuck MackinnonBuck left a comment

Choose a reason for hiding this comment

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

This looks great!

I left some questions and comments but they're nothing worth blocking the PR on.

@javiercn javiercn force-pushed the javiercn/add-static-web-assets-endpoints branch 2 times, most recently from 7d420fa to 51b19d0 Compare May 13, 2024 13:49
@javiercn javiercn force-pushed the javiercn/add-static-web-assets-endpoints branch from 51b19d0 to f89d513 Compare May 13, 2024 13:54
@javiercn javiercn merged commit 51838a2 into main May 13, 2024
26 checks passed
@javiercn javiercn deleted the javiercn/add-static-web-assets-endpoints branch May 13, 2024 16:04
@dotnet-policy-service dotnet-policy-service bot added this to the 9.0-preview5 milestone May 13, 2024
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.

None yet

2 participants