BlazorWasmPreRendering.Build
1.0.0-preview.10.1
See the version list below for details.
dotnet add package BlazorWasmPreRendering.Build --version 1.0.0-preview.10.1
NuGet\Install-Package BlazorWasmPreRendering.Build -Version 1.0.0-preview.10.1
<PackageReference Include="BlazorWasmPreRendering.Build" Version="1.0.0-preview.10.1" />
paket add BlazorWasmPreRendering.Build --version 1.0.0-preview.10.1
#r "nuget: BlazorWasmPreRendering.Build, 1.0.0-preview.10.1"
// Install BlazorWasmPreRendering.Build as a Cake Addin #addin nuget:?package=BlazorWasmPreRendering.Build&version=1.0.0-preview.10.1&prerelease // Install BlazorWasmPreRendering.Build as a Cake Tool #tool nuget:?package=BlazorWasmPreRendering.Build&version=1.0.0-preview.10.1&prerelease
BlazorWasmPreRendering.Build
Summary
When you publish your Blazor WebAssembly app, this package pre-renders and saves the app as static HTML files in your public folder.
This will help make the contents of your Blazor WebAssembly static apps findable in internet search and be visible from the OGP client.
An output of "dotnet publish" before installing this package:
And after installing this package:
Usage
Install this package to your Blazor WebAssembly project.
dotnet add package BlazorWasmPreRendering.Build --version 1.0.0-preview.10.1
Basically, that's all.
Once installing this package is done, the output of the dotnet publish
command will include pre-rendered contents.
Configurations
Services registration
If you are registering any services (except HttpClient that isn't specially configured) to the service provider at the startup of your Blazor WebAssembly app, please extract that process to the static method named static void ConfigureServices(IServiceCollection services, string baseAddress)
.
public class Program
{
public static async Task Main(string[] args)
{
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("#app");
// 👇 Extract service registration...
services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(baseAddress) });
services.AddScoped<IFoo, MyFoo>();
await builder.Build().RunAsync();
}
}
public class Program
{
public static async Task Main(string[] args)
{
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("#app");
ConfigureServices(builder.Services, builder.HostEnvironment.BaseAddress);
await builder.Build().RunAsync();
}
// 👇 ... to this named static method.
private static void ConfigureServices(IServiceCollection services, string baseAddress)
{
services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(baseAddress) });
services.AddScoped<IFoo, MyFoo>();
}
}
And, if you implement the entry point as C# 9 top-level statement style, then you have to also extract the service-registration process to the static local function named static void ConfigureServices(IServiceCollection services, string baseAddress)
.
NOTICE: The "ConfigureServices" local function must be "static" local function.
// The "Program.cs" that is C# 9 top-level statement style entry point.
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("#app");
ConfigureServices(builder.Services, builder.HostEnvironment.BaseAddress);
await builder.Build().RunAsync();
// 👇 extract the service-registration process to the static local function.
static void ConfigureServices(IServiceCollection services, string baseAddress)
{
services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(baseAddress) });
services.AddScoped<IFoo, MyFoo>();
}
Aside: C# 9 top-level statement style entry point can be used for only .NET6 or above.
This package calls the ConfigureServices()
static method (or static local function) inside of your Blazor WebAssembly app when pre-renders it if that method exists.
This is important to your Blazor WebAssembly components work fine in the pre-rendering process.
Root component type and selector
In some cases, suppose the type and selector of the root component of your Blazor WebAssembly app are not {RootNamespace}.App
and #app
or app
.
In that case, you have to describe that information explicitly in the project file (.csproj) of your Blazor WebAssembly app, like this.
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">
...
<PropertyGroup>
<BlazorWasmPrerenderingRootComponentType>My.Custom.RootComponentClass</BlazorWasmPrerenderingRootComponentType>
<BlazorWasmPrerenderingRootComponentSelector>.selector-for-root</BlazorWasmPrerenderingRootComponentSelector>
</PropertyGroup>
...
If the root component doesn't live in the application assembly, you can specify assembly name in the <BlazorWasmPrerenderingRootComponentType>
peoperty value, like this.
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">
...
<PropertyGroup>
<BlazorWasmPrerenderingRootComponentType>My.Custom.RootComponentClass, My.CustomAssembly</BlazorWasmPrerenderingRootComponentType>
...
Note: If the specified type was not found...
If the specified type was not found, as a fallback behavior, this package tries to find the root component type (which has the type name "App" and inherits ComponentBase
type) from all assemblies that referenced from the application assembly.
Appendix
- If you would like to change a title or any meta elements for each page in your Blazor WebAssembly app, I recommend using the "Blazor Head Element Helper" NuGet package.
- Since the ver.1.0.0 preview 8 of this package, the .NET 6
<PageTitle>
and<HeadContent>
components are also statically pre-rendered properly.
- Since the ver.1.0.0 preview 8 of this package, the .NET 6
- If you would like to deploy your Blazor WebAssembly app to GitHub Pages, I recommend using the "Publish SPA for GitHub Pages" NuGet package.
- The "Awesome Blazor Browser" site is one of a good showcase of this package. That site is republishing every day by GitHub Actions with pre-rendering powered by this package.
Notice
This package is now experimental stage.
We can expect this package will work fine with a simple Blazor WebAssembly project.
But I'm not sure this package works fine even with a complicated real-world Blazor WebAssembly project at this time.
I welcome to fork and improve this project on your hand.
License
Learn more about Target Frameworks and .NET Standard.
This package has no dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories (7)
Showing the top 5 popular GitHub repositories that depend on BlazorWasmPreRendering.Build:
Repository | Stars |
---|---|
ant-design-blazor/ant-design-blazor
🌈A rich set of enterprise-class UI components based on Ant Design and Blazor.
|
|
jsakamoto/BlazorWasmPreRendering.Build
When you publish your Blazor Wasm app, this package pre-renders and saves the app as static HTML files in your public folder.
|
|
jsakamoto/awesome-blazor-browser
A Blazor WebAssembly app for browsing the "Awesome Blazor" resources.
|
|
jsakamoto/Toolbelt.Blazor.HeadElement
Head element support (change the document title, "meta" elements such as OGP, and "link" elements) for Blazor apps.
|
|
CropperBlazor/Cropper.Blazor
Cropper.js as Blazor component for cropping images
|
Version | Downloads | Last updated |
---|---|---|
5.0.0 | 369 | 11/12/2024 |
5.0.0-preview.2 | 650 | 9/23/2024 |
5.0.0-preview.1 | 403 | 4/18/2024 |
4.0.1 | 742 | 9/23/2024 |
4.0.0 | 3,243 | 4/18/2024 |
3.1.0-preview.4 | 4,299 | 9/20/2023 |
3.1.0-preview.3 | 169 | 9/14/2023 |
3.1.0-preview.2 | 114 | 9/3/2023 |
3.1.0-preview.1 | 171 | 9/2/2023 |
3.0.0-preview.2 | 404 | 8/3/2023 |
3.0.0-preview.1 | 222 | 7/9/2023 |
2.0.0-preview.9 | 960 | 5/28/2023 |
2.0.0-preview.8 | 1,036 | 3/15/2023 |
2.0.0-preview.7 | 1,045 | 11/22/2022 |
2.0.0-preview.6 | 117 | 11/20/2022 |
2.0.0-preview.5 | 233 | 11/16/2022 |
2.0.0-preview.4 | 617 | 11/3/2022 |
2.0.0-preview.3.1 | 110 | 11/3/2022 |
2.0.0-preview.2 | 135 | 10/23/2022 |
2.0.0-preview.1 | 163 | 10/22/2022 |
1.0.0-preview.28.0 | 707 | 9/22/2022 |
1.0.0-preview.27.0 | 114 | 9/21/2022 |
1.0.0-preview.26.0 | 889 | 7/23/2022 |
1.0.0-preview.25.1 | 123 | 7/23/2022 |
1.0.0-preview.25.0 | 226 | 7/10/2022 |
1.0.0-preview.24.1 | 368 | 6/28/2022 |
1.0.0-preview.24.0 | 1,138 | 4/19/2022 |
1.0.0-preview.23.0 | 190 | 3/16/2022 |
1.0.0-preview.22.0 | 184 | 3/10/2022 |
1.0.0-preview.21.0 | 138 | 3/9/2022 |
1.0.0-preview.20.0 | 123 | 3/9/2022 |
1.0.0-preview.19.0 | 141 | 2/26/2022 |
1.0.0-preview.18.0 | 131 | 2/26/2022 |
1.0.0-preview.17.0 | 1,607 | 2/23/2022 |
1.0.0-preview.16.0 | 150 | 2/18/2022 |
1.0.0-preview.15.0 | 127 | 2/15/2022 |
1.0.0-preview.14.0 | 142 | 1/18/2022 |
1.0.0-preview.13.0 | 214 | 12/18/2021 |
1.0.0-preview.12.0 | 282 | 12/18/2021 |
1.0.0-preview.11.4 | 194 | 12/16/2021 |
1.0.0-preview.10.1 | 271 | 12/11/2021 |
1.0.0-preview.9.0 | 511 | 10/16/2021 |
1.0.0-preview.8.0 | 235 | 9/19/2021 |
1.0.0-preview.7.1 | 296 | 8/24/2021 |
1.0.0-preview.6 | 161 | 7/27/2021 |
1.0.0-preview.5 | 258 | 7/23/2021 |
1.0.0-preview.4.1 | 338 | 5/22/2021 |
1.0.0-preview.4 | 237 | 5/9/2021 |
1.0.0-preview.3 | 193 | 5/9/2021 |
1.0.0-preview.2 | 204 | 5/9/2021 |
v.1.0.0-preview.10.1
Fix: the pre-rendering process crashed at executing publishing on Visual Studio.