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.

StreamRendering and RenderModeServer causes OnInitialized to be invoked twice. Second time query parameter not bound.

See original GitHub issue

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Interactive Server page with attributes StreamRendering(true) and RenderModeServer causes OnInitialized/OnInitializedAsync to be invoked twice.

Taking a normal route parameter and a parameter supplied from query (using SupplyParameterFromQuery).

Invoking the page with fresh navigation using query parameter causes the OnInitialized method to be invoked twice. The second time the query parameter is not bound. Though the regular route parameter is.

Expected Behavior

The parameter should be bound from query.

Question: <strike>Should OnInitialized be invoked twice? I thought only OnAfterRender ran twice while the server component being the same. </strike>

I see, the component object is not being re-used from the pre-render.

Steps To Reproduce

Doing a fresh navigation to: /test/x?queryParam=test

@page "/test/{path?}"
@attribute [StreamRendering(true)]
@attribute [RenderModeServer]

@QueryParam

@code 
{
    [Parameter]
    public string? Param { get; set; }

    [SupplyParameterFromQuery]
    public string? QueryParam { get; set; }

    protected override void OnInitialized()
    {
        // Method called twice
        
        // First time the value of QueryParam is set to "test", and Param is "x"
        // The second time the value of QueryParam is null, but Param is "x"
        Console.WriteLine(QueryParam);
    }
}

First time hitting break-point: Screenshot 2023-07-25 at 13 39 37

Second time: Screenshot 2023-07-25 at 13 40 00

Exceptions (if any)

No response

.NET Version

8.0.100-preview.6.23330.14

Anything else?

No response

Issue Analytics

  • State:closed
  • Created 2 months ago
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
javiercncommented, Jul 25, 2023

@marinasundstrom no, that should work.

I am not sure why is not working, but there has been a lot of churn in this area during preview7, so I would suggest you try a nightly build.

1reaction
javiercncommented, Jul 25, 2023

@marinasundstrom thanks for contacting us.

OnInitialized will execute once during prerendering, and once again when the component becomes interactive. During prerendering a render process starts, and once we are done prerendering, it stops, disposing all the prerendered components.

Then, for components that have either server or webassembly mode, the components are re-created again and produce a new render to become interactive (that’s where the OnInitialized runs for the second time).

Read more comments on GitHub >

github_iconTop Results From Across the Web

8.0-rc2 Milestone
StreamRendering and RenderModeServer causes OnInitialized to be invoked twice. Second time query parameter not bound. area-blazor Includes: Blazor, ...
Read more >
c# - Blazor rendering content twice
After changing to render-mode="Server" I was able to prevent rendering twice on the first load. But now when I refresh the page it...
Read more >
ASP.NET Core Blazor performance best practices
In this article. Optimize rendering speed; Optimize JavaScript interop speed; Ahead-of-time (AOT) compilation; Minimize app download size.
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