PersistComponentState inconsistent behavior #51583
Labels
area-blazor
Includes: Blazor, Razor Components
✔️ Resolution: Answered
Resolved because the question asked by the original author has been answered.
question
Status: Resolved
Is there an existing issue for this?
Describe the bug
I migrated a Blazor hosted WASM app to use .NET 8, and noticed that my database was called twice when navigating to a page that uses WASM render mode and state persistence from a server rendered page. When the page is navigated directly to (or browser is refreshed when on the page), the state persist mechanism works as expected, the database is hit only once, and the markup for state persistence (Blazor-WebAssembly-Component-State) disappears after WASM kicks in.
But after I navigate to another server rendered page and back, the OnInitialized method runs twice without using the state persistance mechanism. This seems like a bug, and puts extra load on the database.
Expected Behavior
I would expect that the state persistence mechanism would behave similarly when navigating between server rendered and interactive WASM pages, and when navigating directly to the interactive page.
Steps To Reproduce
Create new Blazor 8 project with WASM interactivity on per page basis and with sample pages.
Put this code on the Counter page on the Client project:
Run the app and observe the browser Console tab and the application terminal.
When the Counter page is first navigated to, the terminal says "Took count from SERVICE", and the browser says "Took count from STORAGE", which is how it's supposed to work. Same thing happens if the browser is refreshed. Also, the "Blazor-WebAssembly-Component-State" markup disappears.
But when navigating to Home and back to Counter, both say "Took count from SERVICE". This means that the state persistence was not used, and the "database" was hit twice. Also, the "Blazor-WebAssembly-Component-State" markup stays on the page.
Exceptions (if any)
No response
.NET Version
8.0.100-rc.2.23502.2
Anything else?
No response
The text was updated successfully, but these errors were encountered: