Skip to content

Conversation

oroztocil
Copy link
Member

@oroztocil oroztocil commented Oct 9, 2025

Fixes two bugs that prevented hot reload from applying changes in Blazor routes.

Premature change token disposal

A bug introduced in #53750 caused the change token used by RazorComponentEndpointDataSource to be disposed before the callback registered on the token is invoked. This means that UpdateEndpoints is not being called at all during hot reload.

This PR does not remove the token disposal so that the memory leak is not re-introduced. Instead, the PR moves the disposal of the current token to UpdateEndpoints itself. This is valid because the role of the change token is precisely to trigger the update and the token is replaced at the end of UpdateEndpoints with a new one (as before).

Stale route data

The first bug was previously hiding a further problem in how we cache route data used by the main ASP.NET Core routing. This data was computed once during application start up and then reused in every UpdateEndpoints invocation. This means that we did not re-scan the assemblies for added, modified, or removed routes.

The PR changes the implementation of RazorComponentEndpointDataSource and related types so that we do not store a pre-built instance of ComponentApplicationBuilder. Instead we store a list of configuration actions that were invoked for the current RazorComponentEndpointDataSource. When the route data needs to be updated, these actions are replayed on a new ComponentApplicationBuilder instance, thus ensuring an up-to-date state. Note that this follows the same pattern as we already use for endpoint conventions.

Result

After the change, hot reload works as expected in the following scenarios:

  • A route is changed or added for an existing page.
  • A route is added for a new page.

Hot reload still does not work when deleting a page with route.

Fixes #52582

@oroztocil oroztocil requested a review from a team as a code owner October 9, 2025 14:17
@github-actions github-actions bot added the area-blazor Includes: Blazor, Razor Components label Oct 9, 2025
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes two critical bugs in Blazor hot reload functionality for route changes: premature change token disposal and stale route data caching.

  • Moves change token disposal to prevent premature disposal before callbacks are invoked
  • Replaces pre-built ComponentApplicationBuilder instances with configuration actions that are replayed on each update
  • Updates test code to work with the new action-based configuration approach

Reviewed Changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
RazorComponentEndpointDataSource.cs Core fix: replaces cached builder with action replay pattern and moves token disposal timing
RazorComponentsEndpointConventionBuilder.cs Updates to store and expose configuration actions instead of direct builder reference
RazorComponentEndpointDataSourceFactory.cs Changes to use action-based configuration for assembly registration
RazorComponentsEndpointConventionBuilderExtensions.cs Updates assembly addition to use new action pattern
RazorComponentEndpointDataSourceTest.cs Test updates to work with new action-based configuration
HotReloadServiceTests.cs Comprehensive test refactoring to use new pattern and verify token disposal behavior
RazorComponentsEndpointConventionBuilderExtensionsTest.cs Minor test update for new action pattern

@javiercn
Copy link
Member

javiercn commented Oct 9, 2025

/backport to release/10.0

Copy link
Contributor

github-actions bot commented Oct 9, 2025

Started backporting to release/10.0: https://github.com/dotnet/aspnetcore/actions/runs/18383238424

@lewing lewing merged commit 982cae2 into main Oct 9, 2025
30 checks passed
@lewing lewing deleted the oroztocil/blazor-route-hot-reload branch October 9, 2025 18:25
@dotnet-policy-service dotnet-policy-service bot added this to the 11.0-preview1 milestone Oct 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-blazor Includes: Blazor, Razor Components
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Adding a route to a Blazor component doesn't work with hot reload
3 participants