Excubo.Blazor.LazyStyleSheet 1.0.1

There is a newer version of this package available.
See the version list below for details.
Install-Package Excubo.Blazor.LazyStyleSheet -Version 1.0.1
dotnet add package Excubo.Blazor.LazyStyleSheet --version 1.0.1
<PackageReference Include="Excubo.Blazor.LazyStyleSheet" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Excubo.Blazor.LazyStyleSheet --version 1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Excubo.Blazor.LazyStyleSheet, 1.0.1"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install Excubo.Blazor.LazyStyleSheet as a Cake Addin
#addin nuget:?package=Excubo.Blazor.LazyStyleSheet&version=1.0.1

// Install Excubo.Blazor.LazyStyleSheet as a Cake Tool
#tool nuget:?package=Excubo.Blazor.LazyStyleSheet&version=1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Why Excubo.Blazor.LazyStyleSheet?

A major issue on websites is slow page load. In part, this is due to enormous payloads that need to be downloaded in full before a page can be rendered correctly. Minimization and compression help to some degree, but it ignores the awkward fact that many style sheets are only used to a tiny fraction.

With HTTP/2, loading small files rather than one large one is less of a performance concern than with HTTP/1.1. Since Blazor uses HTTP/2 by default, we can make use of this and split style sheets into smaller chunks. Those chunks can then be loaded lazily, i.e. only when a component actually needs it.

Excubo.Blazor.LazyStyleSheet enables you to write dedicated style sheet for each component.

How to use

1. Install the nuget package Excubo.Blazor.LazyStyleSheet

Package Manager:
Install-Package Excubo.Blazor.LazyStyleSheet
.NET Cli:
dotnet add package Excubo.Blazor.LazyStyleSheet
Package Reference
<PackageReference Include="Excubo.Blazor.LazyStyleSheet" />

2. Add the service in your Startup.cs file


3. Add the StyleSheets component to your App component

<Router AppAssembly="@typeof(Program).Assembly">
    <Found Context="routeData">
        <AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
        <LayoutView Layout="@typeof(MainLayout)">
            <p>Sorry, there's nothing at this address.</p>
<Excubo.Blazor.LazyStyleSheet.StyleSheets />

4. Add style sheets where necessary

@page "/hello"
@inject Excubo.Blazor.LazyStyleSheet.IStyleSheetService StyleSheetService

<div class="my-style">My styled component</div>

@code {
    protected override void OnInitialized()
        StyleSheetService.Add("css/my_style.css"); // the StyleSheetService prevents styles from being added more than once

If you're writing a component library, don't forget to prefix the URL with _content/My.Package.Name/. You also need to instruct your users to follow step 2 and 3, or wrap this on behalf of the user.


  • The StyleSheets component should only be used exactly once. However, except for a slight performance penalty, there is likely no issue by doing so. Of course there's no benefit to multiple StyleSheets components either.
  • Style sheet urls may be added any number of times, and will only be added to the DOM once (as duplicate link tags don't achieve anything). This only applies if the url string matches exactly, i.e. there is a difference between https://localhost/css/style.css and css/style.css.

Tips & tricks

For development it might be more convenient to have the css close to the razor component in the file hierarchy. However, for deployment, the css must be part of the static file assets, usually in wwwroot. You can create Component.razor.css files and automatically move them to the wwwroot folder with the following addition to your csproj file:

    <ComponentStyles Include="**\*.razor.css" Exclude="wwwroot\**" />
  <Target Name="CopyStyles" BeforeTargets="AfterBuild">
    <RemoveDir Directories="wwwroot\css\components" />
    <Copy SourceFiles="@(ComponentStyles)" DestinationFiles="@(ComponentStyles->'wwwroot\css\components\%(RecursiveDir)%(Filename)%(Extension)')" />

Remember to adjust the referenced path:



If you encounter any issues or have ideas for new features, please raise an issue in the repository on GitHub.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.1.21 122 11/9/2021
3.1.19 121 9/15/2021
3.1.18 113 8/11/2021
3.1.17 99 7/19/2021
3.1.14 110 4/7/2021
3.1.13 137 3/3/2021
3.1.11 193 1/13/2021
3.1.9 182 11/13/2020
3.1.8 12,882 10/13/2020
3.1.7 1,100 9/30/2020
3.1.5 1,836 9/8/2020
3.1.4 1,947 8/12/2020
3.1.3 1,315 7/24/2020
3.1.2 490 7/20/2020
3.1.1 639 7/14/2020
3.0.0 331 7/10/2020
2.0.9 1,104 6/15/2020
2.0.8 254 6/5/2020
2.0.7 235 5/13/2020
2.0.6 232 5/13/2020
2.0.5 222 5/1/2020
2.0.2 287 5/1/2020
2.0.1 245 4/28/2020
2.0.0 292 3/27/2020
1.0.1 249 3/26/2020
1.0.0 252 3/11/2020