Skip to content

Make Blazor WebAssembly apps fully trimmable #49409

@danroth27

Description

@danroth27

The .NET trimmer statically analyzes your project and removes any unused code. In Blazor we use the .NET trimmer to significantly reduce the publish size of Blazor WebAssembly apps. However, we don't currently use the .NET trimmer to its fullest potential. We currently configure the trimmer in a partial mode, which only trims assemblies that are explicitly marked as trimmable. This typically means that the app assembly itself and many dependencies are not trimmed. This is a conservative setting that optimizes for maintaining functionality over reducing app size.

The default trim mode for the .NET trimmer is to trim everything. This is the mode used by the .NET Native AOT workloads.Analyzers provide warnings if there are code paths in the app that are not safe to trim. Given how sensitive web apps are to download size and load time, it seems like doing full trimming should be the goal for Blazor.

Enabling support for full trimming also is a potential solution to enabling a single project model for Blazor Web Apps that target both server and browser. When you multitarget a project, everything in the project is built for both targets unless you indicated otherwise. This is problematic for browser scenarios because you may potentially bring in dependencies, components, and code into the browser targeted build that are only needed on the server. However, if we could enable full trimming and indicate to the trimmer which components are setup to render on WebAssembly, then the browser build could be aggressively trimmed to remove any unwanted code and dependencies.

Enabling support for full .NET trimming typically means removing any dynamic code path that are based on reflection. We would need to make Blazor apps trimmer friendly by removing this code using techniques like source generators. For example, in Blazor we use reflection as part of the component discovery process and in the render tree builder code generated by the Razor compiler. We're already working on enabling component discovery through source generation. We also have on our backlog to handle component parameters through source generation. There may be other related work items as well.

There is also ecosystem impact of enabling full trimming by default. Many .NET libraries are not setup for trimming and would potentially need to be updated. It will take some time to work through these issues with the Blazor community. Certain features also simply wouldn't work with full trimming. For example, dynamically loading dependencies at runtime that are not known statically. For backwards compatibility, app developers can configure the trimmer to not trim assemblies that are not yet trimmer friendly. We'd need to understand better all the cases that get impacted by full trimming and ensure we provide a reasonable user experience for addressing them.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Pillar: Technical DebtPriority:1Work that is critical for the release, but we could probably ship withoutarea-blazorIncludes: Blazor, Razor ComponentsenhancementThis issue represents an ask for new feature or an enhancement to an existing onefeature-trimming

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions