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] Public API changes to enable compression on Interactive components for Blazor Web #53821

Closed
javiercn opened this issue Feb 5, 2024 · 1 comment · Fixed by #53835
Closed
Labels
api-approved API was approved in API review, it can be implemented area-blazor Includes: Blazor, Razor Components

Comments

@javiercn
Copy link
Member

javiercn commented Feb 5, 2024

Background and Motivation

We've enabled websocket compression on interactive server components in Blazor web. As part of that, we needed new public APIs to configure some of the settings and disable them if needed.

Compression is enabled by default and we don't expect the majority of our customers to change any of the settings.

Proposed API

+ namespace Microsoft.AspNetCore.Components.Server;

+ public class ServerComponentsEndpointOptions()
{
+  public Func<HttpContext!, WebSocketAcceptContext!>? ConfigureWebsocketOptions { get; set; }
+  public ContentSecurityFrameAncestorPolicy { get; set; }
}

+ name Microsoft.AspNetCore.Builder;

+static class ServerRazorComponentsEndpointConventionBuilderExtensions
{
+  RazorComponentsEndpointConventionBuilder .AddInteractiveServerRenderMode(this RazorComponentsEndpointConventionBuilder! builder, Action<ServerComponentsEndpointOptions!>? callback = null) 

Usage Examples

Disable compression

builder.MapRazorComponents<App>().AddServerRenderMode(o => o.ConfigureWebSocketOptions = null)

Configure a more strict frame-ancestors directive policy.

builder.MapRazorComponents<App>().AddServerRenderMode(o => o.ContentSecurityFrameAncestorsPolicy = "'none'")

Alternative Designs

Disable compression

We discarded this approach because we still want to support configuring the parameters for the websocket, and we don't want to introduce two settings (CompressionEnabled and the callback). And given that this is going to be extremely rare, the callback we think is good enough.

builder.MapRazorComponents<App>().AddServerRenderMode(o => o.CompressionEnabled = false)
@javiercn javiercn added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Feb 5, 2024
@dotnet-issue-labeler dotnet-issue-labeler bot added the area-blazor Includes: Blazor, Razor Components label Feb 5, 2024
@halter73
Copy link
Member

halter73 commented Feb 5, 2024

API Review Notes:

  • Setting the ConfigureWebsocketOptions to null disables compression. This is the only way to disable compression.
    • The default is non-null which is unusual. And it's not obvious that the nullness of the callback disables compression.
    • There is a second way. Setting WebSocketAcceptContext.DangerousEnableCompression = false in ConfigureWebsocketOptions. If you override the Func, you need to manually set it to true again.
    • Can ConfigureWebsocketOptions be an Action rather than a Func?
    • Does ConfigureWebsocketOptions need to return a Task?
  • WebSocket should have a capital "S".
  • "'self'" is the default value for ContentSecurityFrameAncestorsPolicy and maps to a Content-Security-Policy: frame-ancestors {ContentSecurityFrameAncestorsPolicy}; header.
    • "frame-ansestors" is plural, so let's make the property match.
  • We have "FrameAncestors" in the property name to indicate you can set other CSP headers manually for other things.
  • Let's make ConfigureWebSocketOptions non-nullable and make (httpContext, webSocketAcceptContext) => Task.CompletedTask the default value.

API Approved!

+ namespace Microsoft.AspNetCore.Components.Server;

+ public class ServerComponentsEndpointOptions()
+ {
+  public Func<HttpContext, WebSocketAcceptContext, Task> ConfigureWebSocketOptions { get; set; }
+  public string? ContentSecurityFrameAncestorsPolicy { get; set; }
+  public bool DisableWebSocketCompression { get; set; }
+ }

+ name Microsoft.AspNetCore.Builder;

+ public static class ServerRazorComponentsEndpointConventionBuilderExtensions
+ {
+  RazorComponentsEndpointConventionBuilder AddInteractiveServerRenderMode(this RazorComponentsEndpointConventionBuilder builder, Action<ServerComponentsEndpointOptions>? callback = null);
+ }

@halter73 halter73 added api-approved API was approved in API review, it can be implemented and removed api-suggestion Early API idea and discussion, it is NOT ready for implementation labels Feb 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-approved API was approved in API review, it can be implemented area-blazor Includes: Blazor, Razor Components
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants
@halter73 @javiercn and others