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.5k stars 10.04k forks source link

Pre DOM patch event listener in Blazor SSR #58630

Open darrelsilveira047 opened 3 weeks ago

darrelsilveira047 commented 3 weeks ago

Hi Blazor Folks,

I’m using Blazor Static Server-Side Rendering (SSR) for my project, where the interactivity is handled exclusively via JavaScript. In my workflow, I have certain JS components (e.g., Tiny Slider) that need to be disposed of before Blazor applies any DOM updates (patches) to avoid conflicts.

Currently, I use a MutationObserver to detect DOM changes after they occur, but this approach is reactive and doesn’t prevent issues that occur during the DOM patching process itself.

Would it be possible to add an event listener or lifecycle hook that fires before Blazor begins DOM patching in SSR mode? This would allow developers to clean up or dispose of JavaScript components before DOM updates take place, similar to how an “unmount” or “pre-patch” hook works in other frameworks.

Thank you !

Originally posted by @darrelsilveira047 in https://github.com/dotnet/aspnetcore/discussions/58596

mkArtakMSFT commented 5 days ago

Thanks for contacting us. We already have support for this and it shipped in .NET 9 earlier today. Looks like we missed to document this. @MackinnonBuck can you please provide info here for @guardrex to use to add documentation for this? Thanks!

MackinnonBuck commented 5 days ago

We would want to extend our docs about Blazor.addEventListener() to include information about the enhancednavigationstart and enhancednavigationend events. Those events run before and after an enhanced navigation, respectively.

https://learn.microsoft.com/aspnet/core/blazor/javascript-interoperability/static-server-rendering?view=aspnetcore-8.0

guardrex commented 4 days ago

@mkArtakMSFT ... The docs PR is at https://github.com/dotnet/AspNetCore.Docs/pull/34101.

I pinged you, @MackinnonBuck, for review.