question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Server-side rendering with Blazor WebAssembly

See original GitHub issue

Is your feature request related to a problem? Please describe.

I have the following requirements for my app:

  1. No WebSockets (so no Blazor Server)
  2. Server-side rendering (very SEO-sensitive app)
  3. 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:

  1. Install the hosted version of blazorwasm template app.
    dotnet new blazorwasm --hosted -o HostedWasm
    
  2. Copy _Hosted.cshtml from blazorserver template to the Pages folder.
  3. Change endpoints.MapFallbackToFile("index.html") to endpoints.MapFallbackToPage("/_Host") in Startup.cs of the server
  4. 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:closed
  • Created 3 years ago
  • Comments:16 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
javiercncommented, Aug 31, 2020

@guardrex sounds fine to me, just want to make sure you have the content available.

1reaction
Andrzej-Wcommented, Aug 23, 2020

@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 on body. 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found