What to do when Hot Reload is very slow in a Blazor project

Whilst Hot Reload is a great feature of Visual Studio, I found that it was very slow in on specific project, often taking 30-60 seconds before the changes were applied.

I opened a support ticket with Microsoft, who pointed out that when a Razor file is converted to a C# file, the size grows quite a bit. For example, in the editor when I was working with one specific file, which had about 500 lines of Razor, the editor was actually performing various operations on a ~4000 line generated C# file. At runtime, that generated file gets even larger (in the editor, all HTML is stripped out).

To see this, I added the following directive to my project file…


…rebuilt and had a look in the obj\Debug\net6.0\generated\Microsoft.NET.Sdk.Razor.SourceGenerators\Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator folder. This contained all the generated files (*.g.cs), and indeed, the two main files in my project had generated C# of around 4000-5000 lines.

So, when designing Blazor components, it’s worth keeping this in mind, and splitting them down into smaller components whenever possible, as this is likely to improve performance when developing. This isn’t always practical, as splitting them down either means each component having its own DbContext, which leads to all sorts of issues with entity tracking, or passing the context from the parent component into the child components. That latter option is NOT a good idea!

Be First to Comment

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.