Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Sep 19, 2025

Fixes #52582

Problem

When using hot reload to change a Blazor component's route template (e.g., changing @page "/counter" to @page "/counter2"), the application would crash with an InvalidOperationException:

Unable to find the provided template '/counter'

This occurred because:

  1. EndpointHtmlRenderer creates RouteData with updated component type but sets the template from stale routeEndpoint.RoutePattern.RawText
  2. During hot reload, the component type gets updated with new routes, but the endpoint pattern still reflects the old route
  3. Router.Refresh() uses this mismatched route data instead of falling back to normal routing
  4. RouteTableFactory.CreateEntry fails because the old template no longer exists on the updated component

Solution

Added validation in EndpointHtmlRenderer to check if the endpoint's route template matches the component's current route attributes before setting the template. When they don't match (hot reload scenario), the template stays null, forcing the router to fall back to normal routing with updated component metadata.

The fix adds an IsValidRouteTemplateForComponent() method that compares the endpoint route pattern against the component's RouteAttribute templates. Only when there's a match does it set the RouteData.Template, otherwise the template remains null and normal routing is used.

Changes

  • EndpointHtmlRenderer.cs: Added route template validation logic and IsValidRouteTemplateForComponent() helper method to prevent setting stale route templates during hot reload scenarios

Testing

  • All existing routing tests continue to pass (Router: 15 passed, RouteTableFactory: 127 passed)
  • No regressions detected in core routing functionality
  • Addresses the root cause by preventing mismatched route data from being cached in the first place

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copy link
Contributor

Greetings human! You've submitted a PR that modifies code that is shared with https://github.com/dotnet/runtime . Please make sure you synchronize this code with the changes in that repo!

@Copilot Copilot AI changed the title [WIP] Investigate the issue described in @dotnet/aspnetcore/issues/52582 In particular see code in RouteTableFactory.cs and RouteTable.cs and other related code. We need to find out how do stale template paths get into the RouteTableFactory.CreateEntry ca... Fix hot reload routing issue when component route templates change Sep 19, 2025
Copilot finished work on behalf of oroztocil September 19, 2025 11:55
@Copilot Copilot AI requested a review from oroztocil September 19, 2025 11:55
@oroztocil
Copy link
Member

@copilot We don't want to catch the exception and return old data. We want to avoid using the old page route at all. You need to investigate the relevant code paths further. Most likely some other cache needs to be cleared during hot reload so that the updated page route is passed into RouteTableFactory.CreateEntry.

Copy link
Contributor Author

Copilot AI commented Sep 19, 2025

@copilot We don't want to catch the exception and return old data. We want to avoid using the old page route at all. You need to investigate the relevant code paths further. Most likely some other cac...

You're absolutely right. I've investigated the code paths further and found that the real issue is in the EndpointRoutingStateProvider which caches stale RouteData during hot reload.

The problem occurs because:

  1. EndpointHtmlRenderer creates RouteData with the current component type but the old route template from routeEndpoint.RoutePattern.RawText
  2. This stale RouteData is cached in EndpointRoutingStateProvider and bypasses normal routing
  3. Router.Refresh() uses the cached route data instead of falling back to updated routing

I've fixed this by adding hot reload support to EndpointRoutingStateProvider to invalidate cached route data when hot reload occurs, forcing the router to use normal routing with updated component metadata. Commit: bb16b3b

@Copilot Copilot AI changed the title Fix hot reload routing issue when component route templates change Fix hot reload routing issue by invalidating cached route data Sep 19, 2025
Copilot finished work on behalf of oroztocil September 19, 2025 12:23
{
public RouteData? RouteData { get; internal set; }
private static volatile bool _cacheInvalidated;
private RouteData? _routeData;
Copy link
Member

@oroztocil oroztocil Sep 19, 2025

Choose a reason for hiding this comment

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

@copilot _routeData is a non-static field yet you treat it as a static cache. This does not seem correct.

Copy link
Member

Choose a reason for hiding this comment

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

@copilot Rework the solution. Investigate how the EndpointRoutingStateProvider is used.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're absolutely right about the flawed design. I've reworked the solution to fix the root cause in EndpointHtmlRenderer instead of using a broken static flag approach.

The new solution validates that the endpoint's route template matches the component's current route attributes before setting the template. When they don't match (hot reload scenario), the template stays null, forcing the router to use normal routing with updated metadata. Commit: caad798

@Copilot Copilot AI changed the title Fix hot reload routing issue by invalidating cached route data Fix hot reload routing issue by validating route templates in EndpointHtmlRenderer Sep 19, 2025
@Copilot Copilot AI requested a review from oroztocil September 19, 2025 13:04
Copilot finished work on behalf of oroztocil September 19, 2025 13:04
@lewing lewing added the feature-hot-reload This issue is related to the Hot Reload feaature label Sep 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Attention: Shared Code Modified feature-hot-reload This issue is related to the Hot Reload feaature
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