Recent enhancements to ASP.NET Core include hot reload and Razor compiler speed improvements. This article looks at these updates and more. In addition, this article describes the new ErrorBoundary component. Hot reload is a great way to change the markup of a page without losing the current app state. Hot reload also supports changes to CSS. CSS Hot Reload detects changes and applies them live to the DOM.
The updated Razor compiler now utilizes C# source generators that run during the compilation process and produce additional files. This change simplifies the compiler and decreases build times. Previously, the Razor compiler produced a separate Views assembly and a set of public types under the AspNetCore namespace. Now, you can use a single method to access any component.
New Hot Reload features make it easy to modify managed source code while debugging. It allows you to update Razor rendering logic in real-time, without affecting the app’s state. You can also enable Hot Reload to execute automatically after a file is saved. This is fast, and works with Blazor apps and Web applications.
Blazor also has several new features with.NET 6. Blazor now supports hot reload and minimal APIs, which allow for rapid feedback cycle loops while working on a Blazor component. Hot Reload is only necessary in rare situations, and you can see any changes in the component almost immediately. You can also use Blazor’s new features with a Blazor project.
Prerendering from server to improve initial load time
A popular method to improve initial load time is to prerender the data that is needed to render the page. This technique buys time by having the server handle the first request before the client renders the page. However, static hosting providers don’t support this technique, and data must be retrieved twice. Prerendering the data will increase the initial load time of your app, but it may not be the most effective solution for your project.
Blazor is an excellent tool for prerendering from the server. This technique uses a static application to display the user interface (UI) before the application downloads the full file. Although this method doesn’t support prerendering at request time, it can be hacked to improve initial load time by using existing prerendering tools. In this tutorial, I’ll walk you through setting up a Blazor WASM project, adding PowerShell/Bash scripts, and GitHub Actions to deploy the application to GitHub Pages.
Razor compiler speed improvement
A major performance improvement is achieved by using Roslyn Source Generators with the Razor compiler. These compilers reduce the amount of time required to compile ASP.NET Core web apps by as much as 50%. By using Roslyn Source Generators, developers can debug their applications in half the time. Microsoft benchmarks have shown that this compiler speed improvement is significant for both small and large projects.
With this improvement, Razor no longer base64-encodes binary data when doing JS interop. Users can now use JS interop to stream binary data, making uploading large files faster and more convenient. In addition, the inputfile component has been updated to take advantage of the JS interop improvements. Users will notice a significant speed improvement in uploading files.
The AOT compilation for Blazor apps increases runtime performance, but a higher size is associated with it. Typically, Blazor web-assembly apps run on the browser using the.NET IL interpreter, which makes the application run slower than the server-side.NET JIT runtime. However, AOT compilation enables native WebAssembly execution, which is quite fast. It can also be a good choice for CPU-intensive tasks.
Another notable speed improvement in the.NET Framework is the use of RyuJIT, the next generation of JIT compiler. This new compiler enables developers to compile code faster than ever before. With RyuJIT, developers can benefit from twice the speed of the previous 64-bit compiler and 30% faster ASP.NET Core in.NET projects. The new compiler also records information about the code execution, which allows it to generate more efficient native machine code.
New ErrorBoundary component
New features include error boundaries and hot reload, which are useful in handling unhandled exceptions. Blazor also offers an ErrorBoundary component that wraps desired content with an error UI whenever an unhandled exception occurs. This component is also available for defining error content. By default, it renders an empty div with an ErrorBoundary CSS class. However, you can customize the error content with the ErrorContent property.
Developers will be able to make use of Hot Reload on Visual Studio 2022 and Visual Studio Code. Hot Reload allows developers to edit managed source code while debugging without having to run a debugger. This new feature is so convenient that it’s possible to set it to automatically execute whenever a new file is saved. It’s also fast and works well for Blazor apps and Web applications.