Server-side rendering with Blazor WebAssembly
See original GitHub issueIs your feature request related to a problem? Please describe.
I have the following requirements for my app:
- No WebSockets (so no Blazor Server)
- Server-side rendering (very SEO-sensitive app)
- I want to use Blazor WebAssembly
I wonder if Blazor can suit them all.
Describe the solution you’d like
It almost works after the following steps:
- Install the hosted version of
blazorwasm
template app.dotnet new blazorwasm --hosted -o HostedWasm
- Copy
_Hosted.cshtml
fromblazorserver
template to thePages
folder. - Change
endpoints.MapFallbackToFile("index.html")
toendpoints.MapFallbackToPage("/_Host")
inStartup.cs
of the server - Change
<script src="_framework/blazor.server.js"></script>
to<script src="_framework/blazor.webassembly.js"></script>
in_Host.cshtml
Now if you dotnet run
the server you will be able to navigate template pages with prerendered HTML.
But there is a problem: there is no way to initialize the state of components before the first render. For example, if I store counter value in the memory cache and save it on each increment, then after page refresh I will see pre-rendered value from the cache… and then 0 (uninitialized component state)
Issue Analytics
- State:
- Created 3 years ago
- Comments:16 (10 by maintainers)
Top Results From Across the Web
Exploring Blazor Changes in .NET 8 - Server Side ...
You can go for Blazor Server, where an open socket connection is required for each user 'session', and all UI logic executes on...
Read more >ASP.NET Core Blazor hosting models
Razor components can run server-side in ASP.NET Core (Blazor Server) versus client-side in the browser on a WebAssembly-based .
Read more >Server-Side Blazor Component Rendering ('Blazor United') ...
NET 8 Preview 3 introduces server-side rendering support for Blazor components, part of a project that Microsoft has dubbed "Blazor United.".
Read more >Server Side Render Blazor Client Side App (WASM)
Net Core for writing client side applications. I'm interested in the client model that uses WebAssembly to allow Single Page Applications ...
Read more >Enabling prerendering for Blazor WebAssembly apps
In this post I describe the steps to enable server-prerendering for a Blazor WebAssembly application. This post serves as an introduction to ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@guardrex sounds fine to me, just want to make sure you have the content available.
@mifopen you can look here https://github.com/Andrzej-W/BlazorWebAssemblyWithPrerendering This is my very old experiment with Blazor and it will not work now, but you can find a solution for screen flickering after initial prerendering. “Magic” is here: https://github.com/Andrzej-W/BlazorWebAssemblyWithPrerendering/blob/master/BlazorWebAssemblyWithPrerendering.Client/Pages/FetchData.razor Of course it is possible to implement a similar algorithm in the layout and eliminate flickering in a whole application.
Issues related to my experiment https://github.com/dotnet/aspnetcore/issues/14836, https://github.com/dotnet/aspnetcore/issues/14956
Please note that this is used to eliminate screen flickering only and not to transfer state. To transfer very small state I use
data-xxx
attributes onbody
. On the client I use JS interoperability to read attribute value. You can use similar technique to transfer more data - simply embed some JSON text during prerendering.