Skip to content

Avoid completely replacing the DOM when Blazor components are re-rendered after prerendering #42561

Description

@javiercn

Right now, after prerendering we replace the entire contents of the prerendered component with a fresh DOM copy created from scratch. This has several bad side-effects like:

  • Videos are stopped and restarted
  • Animations can be retriggered
  • The page can flicker if CSS is injected as part of the head contents.
  • LCP score suffers because (even though there is no significant visual change in most cases) the tools detect the interactive DOM insertion as the LCP element.

Instead of replacing the contents of the DOM wholesale, we can do a best effort to reconciliate the DOM with the new rendered DOM when we are trying to apply the render batch.

In an ideal scenario, a developer should be able to produce exact prerenders, meaning that the render after prerendering is identical to the prerendered one.

In that case, the renderer would only need to "attach" event handlers to the appropriate nodes, leaving everything else the same.

The renderer does not have to be perfect and handle all possible scenarios, it can impose strict limitations and "bail out" if it can't figure out how to reconcile a given DOM subtree by replacing that node wholesale.

For example, the render can impose a strict order, suggesting that DOM children nodes in a given subtree must appear in the same order as the prerendered node (in other words, the renderer won't search for them, but iterate in order). A similar restriction might be imposed on attributes (which is not rare, since attributes are emitted in compilation order, except for splatting). Things like @key could be leveraged during prerendering too to offer a bit more flexibility in the ordering.

In general, this will likely result in a much higher node reuse and will give the developers the chance to always get this right.

Activity

  1. added
    area-blazor
    feature-prerendering
    on Jul 4, 2022
  2. added this to the .NET 7 Planning milestone on Jul 4, 2022
  3. added
    enhancement
    on Sep 20, 2022
  4. added
    Priority:1
    on Nov 10, 2022
  5. 3hxx commented on Nov 21, 2022

    @3hxx

    Yes yes yes. This is long overdue my guys 🙏😍😇

  6. 67 remaining items

  7. removed their assignment
    on Aug 26, 2025
  8. NearNihil commented on Nov 27, 2025

    @NearNihil

    I've not really done much with animations before, but now I am and it's really noticeable. Is it so much to ask to fix this? It's been years and it's causing headaches all round.

  9. ice6 commented on Jan 17, 2026

    @ice6

    this issue is good showcase about github copilot's inability. :)

  10. Ali1Jamal commented on Mar 9, 2026

    @Ali1Jamal

    also moving from static page to interactive page or vice versa, when the theme of the browser or os is different from the website or app theme ( i add pwa ability to my blazor web app ), causes this flashing because of replacing Dom and reload all assets.

    WhatsApp.Video.2026-03-09.at.17.35.47.mp4
  11. BrendanRidenour commented on Apr 11, 2026

    @BrendanRidenour
    Contributor

    Adding diagnostic evidence from .NET 10 with InteractiveWebAssembly that may be useful for anyone investigating this.

    We confirmed that static HTML elements in App.razor (outside any component boundary) are destroyed and recreated during the SSR-to-WASM handoff. Specifically, a <link rel="stylesheet"> that is plain markup in <head>, not rendered by any Blazor component.

    How we confirmed it: We set all three DOM breakpoints (subtree modified, node removed, attribute modified) on the <link> element in Edge DevTools before triggering the navigation. After the WASM handoff completed, the breakpoints were gone without ever firing. This indicates the node was replaced as part of a wholesale parent replacement rather than individual node operations, which bypasses per-node mutation observers.

    This means the wholesale DOM replacement described in the original issue extends beyond Blazor-managed component subtrees to static HTML that Blazor does not own.

    Workarounds evaluated:

    • data-permanent: only applies to enhanced navigation, not render mode transitions.
    • <link rel="preload">: resource stays cached, but the FOUC still occurs because the DOM node itself is destroyed and recreated.
    • Inline <style> fallback: blocked by Content Security Policy in our environment.
    • Element ordering in <head>: no effect.

    We have no remaining workaround available to us.

  12. cbordeman commented on Apr 11, 2026

    @cbordeman

    This is why everyone says Blazor is garbage now. It's nearly unusable.

  13. pr8x commented on May 25, 2026

    @pr8x

    .NET 11 is coming up and this is nowhere to be found on the roadmap.

  14. ilonatommy commented on Nov 3, 2026

    @ilonatommy
    Member

    Let me summarize the pain points listed in this issue:

    1. [Solved] Text typed before activation disappears. This is already solved by state persistance.
    2. Restarting of animations/videos/carrousels/nested lists scrolling/focus.
    3. Visible flash of the page on hydration.
    4. Largest Contentful Paint web performance measurement is affected which is a part of search ranking e.g. in Google engine.
    5. Stylesheet nodes outside interactive components are replaced - this does not look connected to DOM nodes replacements and should be reported separately with reproduction steps.
    6. Buttons are not interactive during prerendering - this is not connected to the DOM replacement and looks like Add Blazor component event handling to Blazor SSR #56104 cold resolve it.

    Replacing the DOM was a deliberate design choice taken at the time when prerendering to interactive transition didn't exist. Preserving the existing DOM when the prerendered and first interactive output match would solve 2-4. In the case of mismatch, we would fall back to today's whole-root replacement. That should make the work relatively small but would be already a great improvement for majority of the scenarios.
    Here is the draft PR with the proposal: #70270

  15. ilonatommy commented on Nov 4, 2026

    @ilonatommy
    Member

    @Ali1Jamal or anyone who experienced the same problem. I cannot reproduce your report that I listed as point 3. A smallest reproduction app would be very helpful to make sure the issue is indeed related and not e.g. a variation of #64566 or a navigation problem.

  16. Ali1Jamal commented on Nov 4, 2026

    @Ali1Jamal

    @ilonatommy nav like the video above from home to shopping card or vice versa make sure to make the website theme different from os theme to see clearly, in the ios Safari its very clear you can test the website from this link production demo

  17. ilonatommy commented on Nov 4, 2026

    @ilonatommy
    Member

    109. @ilonatommy nav like the video above from home to shopping card or vice versa make sure to make the website theme different from os theme to see clearly, in the ios Safari its very clear you can test the website from this link production demo

    Thank you. Maybe the phrasing "smallest reproduction app" was unclear. I meant code of the application and, in case your app's code cannot be public, reproducing the same issue on a template app and providing us with a link to the repo with that template app. If you prefer not to share it here and you are not able to create the smallest repro app but you still want us to take a look, you can email the relevant private code to itomkowicz@microsoft.com.
    Otherwise, it will be hard to reason about the issue without the code.

  18. ilonatommy commented on Nov 4, 2026

    @ilonatommy
    Member

    I made measurements of LCP and there seems to be no improvement when looking at a median and average values of 100 loads of an image, regardless if we replace the DOM or not.

    Renderer Mean [ms] Median [ms] p75 [ms] p95 [ms]
    Original 5361.3 5360 5372 5392.6
    Fixed 5367.8 5368 5384 5400.2

    It's mostly due to the fact that LCP records the time when the largest visible content finished painting and doesn't care what happens to it later (if hydration will replace it with a new node or not). If the item doesn't get fully loaded during static rendering, loading in interactivity reuses the existing in-flight request rather than loading from 0.

    It doesn't make this issue less important, I am just flagging that my initial claim that the fix will improve LCP when collecting the list of affected scenarios was not correct.

  19. jirisykora83 commented on Nov 4, 2026

    @jirisykora83

    29. I made measurements of LCP and there seems to be no improvement when looking at a median and average values of 100 loads of an image, regardless if we replace the DOM or not.

     Renderer
     Mean [ms]
     Median [ms]
     p75 [ms]
     p95 [ms]
     
     
     
     
     Original
     5361.3
     5360
     5372
     5392.6
     
     
     Fixed
     5367.8
     5368
     5384
     5400.2
     
     
     
     It's mostly due to the fact that LCP records the time when the largest visible content finished painting and doesn't care what happens to it later (if hydration will replace it with a new node or not). If the item doesn't get fully loaded during static rendering, loading in interactivity reuses the existing in-flight request rather than loading from 0.
     It doesn't make this issue less important, I am just flagging that my initial claim that the fix will improve LCP when collecting the list of affected scenarios was not correct.
    

    One edge case worth considering regarding LCP is when the primary candidate (e.g. hero image or dynamic heading) cannot be prerendered at all—such as when it depends on client-side state (localStorage, auth tokens, geolocation). In those cases, the server only renders a placeholder, and the actual LCP element paints only after hydration kicks in.

    That said, this is likely only noticeable on pages with a larger/heavier DOM. In an isolated benchmark, tearing down and recreating nodes takes negligible time. On a complex DOM tree, however, the main-thread blocking time from a full replacement (style recalculation, layout thrashing, GC pressure) could meaningfully delay hydration and push back that final paint.

  20. self-assigned this
    on Nov 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Pillar: Technical DebtPriority:1area-blazorenhancement

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions