dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.16k stars 9.92k forks source link

Provide a simple opt-out option of preserving statically rendered components during navigation #56961

Open IevgeniiKunshchykov opened 1 month ago

IevgeniiKunshchykov commented 1 month ago

Is there an existing issue for this?

Describe the bug

The problem is : when I refresh static page that contains interactive component then interactive component is not refreshed.

I've got an app : image image image

When I click remove last button and refresh page using such code : var absoluteUri = this.NavigationManager.ToAbsoluteUri(this.NavigationManager.Uri); this.NavigationManager.NavigateTo(absoluteUri.AbsolutePath);

Then page is refreshed but interactive component not image

If I provide a @key to my interactive component inside static one image

Then all works as expected and interactive component is recreated and displayed image

Here is link to repo https://github.com/IevgeniiKunshchykov/InteractivePageUpdateInsideStatic.git

Can someone explain me that behavior? Why is that done in such way? Why interactive component is not reinitialized without @key?

Thank you!

Expected Behavior

Interactive component is reinitialized inside static one

Steps To Reproduce

No response

Exceptions (if any)

No response

.NET Version

No response

Anything else?

No response

mkArtakMSFT commented 1 month ago

Thanks for contacting us. The behavior you're observing is by-design. What is happening here is that Blazor has some special features that are available to statically rendered components. One of these features is the DOM Merging logic, which is used to maintain as much unchanged content from the previous page as possible. That logic utilizes the @key directive to match components. That's why, when you choose to use different keys the content is being replaced and you get the desired behavior.

I'll update the title of this issue and we'll use this going forward to see if we need to come up with some simpler / more intuitive way of opting-out from this behavior. Something like @do-not-preserve (bad name probably, but we'll come up with a better one later).

garrettlondon1 commented 1 month ago

@mkArtakMSFT I understand it was initially by design but its very weird telling a parent static page to refresh, and an interactive island inside of that parent page does not reinitialize

If you forceload the navigation manager it will reopen a new circuit

I think the default navigation manager behavior should reinitialize all interactive components by default when a parent static page is reloaded

The @key has no use on a static page, neither does @do-not-preserve, we should opt in to @preserve