Skip to content

[Blazor] Document DOM listener lifecycle ownership - #68823

Merged
PureWeen merged 2 commits into
dotnet:mainfrom
PureWeen:pureween-codify-blazor-lifecycle-guidance
Sep 2, 2026
Merged

PureWeen merged 2 commits into
dotnet:mainfrom
PureWeen:pureween-codify-blazor-lifecycle-guidance

Conversation

@PureWeen

@PureWeen PureWeen commented Aug 26, 2026 •

Copy link
Copy Markdown
Member

Summary

  • add Components-specific guidance for JavaScript behavior whose target DOM can be replaced during the prerender-to-interactive transition or enhanced navigation
  • keep element-specific listeners with the narrowest viable lifecycle owner and require cleanup when that owner is removed
  • require lifecycle regression tests to exercise the render boundary that owns the relevant DOM
  • route behavior owned by generated template content to the existing generated-template browser tests instead of a parallel Components E2E surface

Rationale

#68677 exposed a listener attached once to prerendered menu DOM that was later replaced by the interactive renderer. Both #68768 and the first revision of #68809 reached for document-level delegation. Maintainer feedback on #68768 explicitly rejected a global listener for element-specific behavior and requested the narrowest practical scope.

The current #68809 direction uses custom-element connection and disconnection callbacks, matching the lifecycle-owned pattern already used by Blazor client validation in #67324.

The regression-test investigation exposed a related boundary issue: the template test matrix defaults to per-page interactivity, which can leave the surrounding layout and navigation menu static even when the selected page is interactive. An explicit all-interactive row was required to prove that the target DOM was replaced. Permanent coverage also belongs in the existing generated-template browser tests because the behavior is owned by generated template content.

The guidance remains intentionally scoped to Components. It does not require every listener to use a custom element, every lifecycle test to use global interactivity, or every template-related behavior to move out of Components E2E; those choices follow the ownership boundary of the specific behavior under test.

@PureWeen
PureWeen marked this pull request as ready for review August 26, 2026 17:38
@PureWeen
PureWeen requested a review from a team as a code owner August 26, 2026 17:38
Copilot AI lite review requested due to automatic review settings August 26, 2026 17:38

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds Components-area guidance on owning and cleaning up JavaScript event listeners when the target DOM can be replaced (e.g., during prerender-to-interactive transition or enhanced navigation), with an emphasis on using the narrowest viable lifecycle boundary and avoiding unnecessary document/window listeners.

Changes:

  • Document a Components-specific rule of thumb for DOM listener lifecycle ownership under prerendering and enhanced navigation.
  • Clarify that element-scoped behavior should be owned by the narrowest durable boundary and cleaned up on removal, reserving global listeners for truly global behavior.
Show a summary per file
File Description
src/Components/AGENTS.md Adds JavaScript DOM lifecycle guidance for listener ownership/cleanup under DOM replacement scenarios.

Review details

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

  • Files reviewed: 1/1 changed files
  • Comments generated: 0
  • Review effort level: Lite

@dariatiurina dariatiurina left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great addition!

Copilot AI added 2 commits August 27, 2026 09:32
Guide element-specific JavaScript behavior toward scoped lifecycle owners across prerender-to-interactive transitions and enhanced navigation, while preserving global listeners for genuinely global behavior.

Co-authored-by: Copilot App <[email protected]>
Require lifecycle-sensitive tests to exercise the render boundary that owns the relevant DOM, and route template-owned behavior to the existing generated-template browser tests.

Co-authored-by: Copilot App <[email protected]>
@PureWeen
PureWeen force-pushed the pureween-codify-blazor-lifecycle-guidance branch from 9aab4ec to df59f5e Compare August 27, 2026 14:33
@PureWeen
PureWeen merged commit 456cc3d into dotnet:main Sep 2, 2026
16 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Sep 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants