Improve Sections warning messaging#67977
Open
dariatiurina wants to merge 5 commits into
Open
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Adds diagnostic logging for Blazor Sections (SectionOutlet/SectionContent) to warn about render mode mismatches and to emit deferred debug diagnostics for orphaned outlets/contents, with unit/integration test coverage.
Changes:
- Add
SectionRegistrydiagnostics: warn on outlet/content render mode mismatches and emit debug logs for orphaned section halves (deferred to end-of-batch). - Plumb renderer logging access via
Renderer.LoggerFactoryandRenderHandle.LoggerFactory, and flush deferred section diagnostics after render batches. - Add unit tests (
SectionRegistryTest) and anEndpointHtmlRendererintegration test + razor test component.
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| src/Components/Components/src/Sections/SectionRegistry.cs | Adds logger setup, log-once latches, mismatch detection, and deferred orphan diagnostics flushed at end-of-batch. |
| src/Components/Components/src/Sections/SectionOutlet.cs | Captures outlet render mode at attach and ensures the registry has a logger. |
| src/Components/Components/src/Sections/SectionContent.cs | Captures content render mode at attach and ensures the registry has a logger. |
| src/Components/Components/src/RenderTree/Renderer.cs | Retains ILoggerFactory, exposes it internally, and flushes section diagnostics after render batches. |
| src/Components/Components/src/RenderHandle.cs | Exposes the renderer’s ILoggerFactory to components via RenderHandle. |
| src/Components/Components/src/Dispatcher.cs | Adds SectionRegistryIfExists to avoid allocating a registry when sections aren’t used. |
| src/Components/Components/test/Sections/SectionRegistryTest.cs | New unit tests for mismatch/orphan diagnostics driven via a test renderer. |
| src/Components/Endpoints/test/EndpointHtmlRendererTest.cs | Adds integration test asserting mismatch warning is logged during prerendering; updates helper to accept a logger factory. |
| src/Components/Endpoints/test/TestComponents/SectionOutletAndContentWithDifferentRenderModes.razor | New test component to create a static outlet + interactive content mismatch scenario. |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
3 tasks
ilonatommy
reviewed
Jul 24, 2026
ilonatommy
reviewed
Jul 24, 2026
Comment on lines
+109
to
+110
| // Option A: the active content for this section changed. If a matching outlet exists in a | ||
| // different render mode, warn now. |
Member
There was a problem hiding this comment.
Suggested change
| // Option A: the active content for this section changed. If a matching outlet exists in a | |
| // different render mode, warn now. |
|
|
||
| private HashSet<object>? _identifiersPendingOrphanCheck; | ||
|
|
||
| // Kept as separate "already logged" sets so a section that changes problem type still emits the new diagnostic. Concrete case: during streaming SSR a SectionOutlet first renders orphaned (its SectionContent hasn't streamed in yet), then the SectionContent streams in under a different render mode; the mismatch warning must still fire even though the orphan was already logged. |
Member
There was a problem hiding this comment.
Suggested change
| // Kept as separate "already logged" sets so a section that changes problem type still emits the new diagnostic. Concrete case: during streaming SSR a SectionOutlet first renders orphaned (its SectionContent hasn't streamed in yet), then the SectionContent streams in under a different render mode; the mismatch warning must still fire even though the orphan was already logged. |
| internal ComponentsMetrics? ComponentMetrics => _componentsMetrics; | ||
| internal ComponentsActivitySource? ComponentActivitySource => _componentsActivitySource; | ||
|
|
||
| internal ILoggerFactory LoggerFactory => _loggerFactory; |
| providers.Add(provider); | ||
| } | ||
|
|
||
| MarkForOrphanCheck(identifier); |
Member
There was a problem hiding this comment.
The issue doesn't mention orphan checks. From what I see orphans are supported pattern, e.g. we have tests in src/Components/test/E2ETest/Tests/SectionsTest.cs that check that pattern as passing. This change should not be in the PR scope.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Improve Sections warning messaging
Summary
Adds diagnostic logging to the Blazor Sections feature (
SectionOutlet/SectionContent) so developers are told when a section is misconfigured — most importantly when the outlet and content are split across a render mode boundary, which Sections does not support. This is purely observational; it does not change how sections resolve or render.Changes
LogLevel.Warning,SectionRenderModeMismatch): emitted when aSectionOutletand a matchingSectionContentare both known to a single registry but declare different render modes. The message explains that sections cannot connect across render mode boundaries and the outlet will not display the content once the components become interactive.LogLevel.Debug):SectionOutletWithoutContentandSectionContentWithoutOutletare emitted when only one half of a section is present. These messages are purely factual (no render mode text), since the common cause is a missing/mismatched section ID rather than a render mode boundary.Details
SectionRegistrynow lives on theRendererinstead of theDispatcher.Rendererretains theILoggerFactoryit is constructed with and creates the registry lazily on first use (SectionRegistry => _sectionRegistry ??= new SectionRegistry(_loggerFactory)), so renderers that never use Sections allocate nothing.RenderHandleexposes the renderer's registry (RenderHandle.SectionRegistry, mirroring the existingComponentMetrics/ComponentActivitySourceaccessors), andSectionOutlet/SectionContentobtain it from the render handle when they attach, capturing their render mode at attach time (a component's render mode is stable for its lifetime). The registry receives the factory through its constructor and builds its own logger, so these renderer-level diagnostics route through the renderer's own logging pipeline and require no logger to flow through theDispatcher.SubscribeandNotifyContentProviderChanged). Render modes are compared by type so two instances of the same mode (e.g. differing only by prerender flag) are treated as equivalent, while static (null) is distinct from any interactive mode.AddProvider/RemoveProvider/Subscribe/Unsubscribe) mark the identifier for an orphan check that is evaluated at end-of-batch viaRenderercallingOnRenderBatchCompleted()on the registry (a no-op when the registry was never created, i.e. Sections are not in use). Deferring to end-of-batch avoids false positives from transient one-sided states during a batch (e.g. a section-id change is a remove+add pair).SectionOutletmay first render orphaned (itsSectionContenthasn't streamed in yet), then have content stream in under a different render mode, and that mismatch warning must still fire.Testing
SectionRegistryTest(new unit tests, driven through a testRenderer): outlet-then-content and content-then-outlet mismatch, static-outlet + interactive-content mismatch, matching modes producing no warning/orphan, and orphaned outlet / orphaned content Debug diagnostics.EndpointHtmlRendererTest.SectionOutletAndContentInDifferentRenderModes_LogsWarningDuringPrerendering(new integration test): prerenders a component with a staticSectionOutletand anInteractiveServerSectionContentthrough the realEndpointHtmlRendererwith aTestSink, and asserts the mismatch warning is logged during SSR. Backed by a newSectionOutletAndContentWithDifferentRenderModes.razortest component.Fixes #66831