-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Fix hot reload routing issue by validating route templates in EndpointHtmlRenderer #63737
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
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! |
Co-authored-by: oroztocil <[email protected]>
Co-authored-by: oroztocil <[email protected]>
RouteTableFactory.CreateEntry
ca...
@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 |
Co-authored-by: oroztocil <[email protected]>
You're absolutely right. I've investigated the code paths further and found that the real issue is in the The problem occurs because:
I've fixed this by adding hot reload support to |
Co-authored-by: oroztocil <[email protected]>
{ | ||
public RouteData? RouteData { get; internal set; } | ||
private static volatile bool _cacheInvalidated; | ||
private RouteData? _routeData; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
…ributes Co-authored-by: oroztocil <[email protected]>
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 anInvalidOperationException
:This occurred because:
EndpointHtmlRenderer
createsRouteData
with updated component type but sets the template from stalerouteEndpoint.RoutePattern.RawText
Router.Refresh()
uses this mismatched route data instead of falling back to normal routingRouteTableFactory.CreateEntry
fails because the old template no longer exists on the updated componentSolution
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 staysnull
, 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'sRouteAttribute
templates. Only when there's a match does it set theRouteData.Template
, otherwise the template remainsnull
and normal routing is used.Changes
IsValidRouteTemplateForComponent()
helper method to prevent setting stale route templates during hot reload scenariosTesting
💡 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.