|
| 1 | +--- |
| 2 | +title: ASP.NET Core Blazor app download size performance best practices |
| 3 | +author: guardrex |
| 4 | +description: Tips for reducing app download size in ASP.NET Core Blazor apps and avoiding common performance problems. |
| 5 | +monikerRange: '>= aspnetcore-3.1' |
| 6 | +ms.author: riande |
| 7 | +ms.custom: mvc |
| 8 | +ms.date: 04/16/2025 |
| 9 | +uid: blazor/performance/app-download-size |
| 10 | +--- |
| 11 | +# ASP.NET Core Blazor app download size performance best practices |
| 12 | + |
| 13 | +[!INCLUDE[](~/includes/not-latest-version.md)] |
| 14 | + |
| 15 | +:::moniker range=">= aspnetcore-6.0" |
| 16 | + |
| 17 | +## Runtime relinking |
| 18 | + |
| 19 | +For information on how runtime relinking minimizes an app's download size, see <xref:blazor/tooling/webassembly#runtime-relinking>. |
| 20 | + |
| 21 | +:::moniker-end |
| 22 | + |
| 23 | +## Use `System.Text.Json` |
| 24 | + |
| 25 | +Blazor's JS interop implementation relies on <xref:System.Text.Json>, which is a high-performance JSON serialization library with low memory allocation. Using <xref:System.Text.Json> shouldn't result in additional app payload size over adding one or more alternate JSON libraries. |
| 26 | + |
| 27 | +For migration guidance, see [How to migrate from `Newtonsoft.Json` to `System.Text.Json`](/dotnet/standard/serialization/system-text-json-migrate-from-newtonsoft-how-to). |
| 28 | + |
| 29 | +## Intermediate Language (IL) trimming |
| 30 | + |
| 31 | +*This section only applies to client-side Blazor scenarios.* |
| 32 | + |
| 33 | +:::moniker range=">= aspnetcore-5.0" |
| 34 | + |
| 35 | +Trimming unused assemblies from a Blazor WebAssembly app reduces the app's size by removing unused code in the app's binaries. For more information, see <xref:blazor/host-and-deploy/configure-trimmer>. |
| 36 | + |
| 37 | +:::moniker-end |
| 38 | + |
| 39 | +:::moniker range="< aspnetcore-5.0" |
| 40 | + |
| 41 | +[Linking a Blazor WebAssembly app](xref:blazor/host-and-deploy/configure-linker) reduces the app's size by trimming unused code in the app's binaries. The Intermediate Language (IL) Linker is only enabled when building in `Release` configuration. To benefit from this, publish the app for deployment using the [`dotnet publish`](/dotnet/core/tools/dotnet-publish) command with the [-c|--configuration](/dotnet/core/tools/dotnet-publish#options) option set to `Release`: |
| 42 | + |
| 43 | +```dotnetcli |
| 44 | +dotnet publish -c Release |
| 45 | +``` |
| 46 | + |
| 47 | +:::moniker-end |
| 48 | + |
| 49 | +## Lazy load assemblies |
| 50 | + |
| 51 | +*This section only applies to client-side Blazor scenarios.* |
| 52 | + |
| 53 | +Load assemblies at runtime when the assemblies are required by a route. For more information, see <xref:blazor/webassembly-lazy-load-assemblies>. |
| 54 | + |
| 55 | +## Compression |
| 56 | + |
| 57 | +*This section only applies to Blazor WebAssembly apps.* |
| 58 | + |
| 59 | +When a Blazor WebAssembly app is published, the output is statically compressed during publish to reduce the app's size and remove the overhead for runtime compression. Blazor relies on the server to perform content negotiation and serve statically-compressed files. |
| 60 | + |
| 61 | +After an app is deployed, verify that the app serves compressed files. Inspect the **Network** tab in a browser's [developer tools](https://developer.mozilla.org/docs/Glossary/Developer_Tools) and verify that the files are served with `Content-Encoding: br` (Brotli compression) or `Content-Encoding: gz` (Gzip compression). If the host isn't serving compressed files, follow the instructions in <xref:blazor/host-and-deploy/webassembly/index#compression>. |
| 62 | + |
| 63 | +## Disable unused features |
| 64 | + |
| 65 | +*This section only applies to client-side Blazor scenarios.* |
| 66 | + |
| 67 | +Blazor WebAssembly's runtime includes the following .NET features that can be disabled for a smaller payload size. |
| 68 | + |
| 69 | +Blazor WebAssembly carries globalization resources required to display values, such as dates and currency, in the user's culture. If the app doesn't require localization, you may [configure the app to support the invariant culture](xref:blazor/globalization-localization#invariant-globalization), which is based on the `en-US` culture. |
| 70 | + |
| 71 | +:::moniker range=">= aspnetcore-8.0" |
| 72 | + |
| 73 | +Adopting [invariant globalization](xref:blazor/globalization-localization#invariant-globalization) only results in using non-localized timezone names. To trim timezone code and data from the app, apply the `<InvariantTimezone>` MSBuild property with a value of `true` in the app's project file: |
| 74 | + |
| 75 | +```xml |
| 76 | +<PropertyGroup> |
| 77 | +<InvariantTimezone>true</InvariantTimezone> |
| 78 | +</PropertyGroup> |
| 79 | +``` |
| 80 | + |
| 81 | +> [!NOTE] |
| 82 | +> [`<BlazorEnableTimeZoneSupport>`](xref:blazor/performance/app-download-size#disable-unused-features) overrides an earlier `<InvariantTimezone>` setting. We recommend removing the `<BlazorEnableTimeZoneSupport>` setting. |
| 83 | +
|
| 84 | +:::moniker-end |
| 85 | + |
| 86 | +:::moniker range="< aspnetcore-8.0" |
| 87 | + |
| 88 | +A data file is included to make timezone information correct. If the app doesn't require this feature, consider disabling it by setting the `<BlazorEnableTimeZoneSupport>` MSBuild property to `false` in the app's project file: |
| 89 | + |
| 90 | +```xml |
| 91 | +<PropertyGroup> |
| 92 | +<BlazorEnableTimeZoneSupport>false</BlazorEnableTimeZoneSupport> |
| 93 | +</PropertyGroup> |
| 94 | +``` |
| 95 | + |
| 96 | +:::moniker-end |
| 97 | + |
| 98 | +:::moniker range="< aspnetcore-5.0" |
| 99 | + |
| 100 | +Collation information is included to make APIs such as <xref:System.StringComparison.InvariantCultureIgnoreCase?displayProperty=nameWithType> work correctly. If you're certain that the app doesn't require the collation data, consider disabling it by setting the `BlazorWebAssemblyPreserveCollationData` MSBuild property in the app's project file to `false`: |
| 101 | + |
| 102 | +```xml |
| 103 | +<PropertyGroup> |
| 104 | +<BlazorWebAssemblyPreserveCollationData>false</BlazorWebAssemblyPreserveCollationData> |
| 105 | +</PropertyGroup> |
| 106 | +``` |
| 107 | + |
| 108 | +:::moniker-end |
0 commit comments