Blazor - Navigating to same [Authorized] page should not recreate page component
- Dominant language
- C#
- Stars
- 38.4k
- Forks
- 10.9k
- Avg merge
- 2d 6h
- Merged PRs (30d)
- 290
Description
### Is there an existing issue for this?
- [x] I have searched the existing issues
### Is your feature request related to a problem? Please describe the problem.
Currently, if a page uses the `[Authorized(Policy="...")]` attribute with a custom, async policy, the page component will be recreated if the user navigates to the same page (i.e. when updating query parameters via `navigation.NavigateTo(navigation.GetUriWithQueryParameter("param", "value))`).
This causes a full page re-render, a repeat of any expensive operations performed in the component's `OnInitialized{Async}` methods, and a loss of component state that isn't tracked by query parameters.
---
There are currently some hacky workarounds:
- Removing the `[Authorize]` attribute, and wrapping the page's contents in an `` component.
- Problem: If the page's initialization logic depends on authorization passing, you'll need to create a separate "real page" component.
- Using the JavaScript interop to call `history.PushState` to update the URL's query parameters.
- Problem: This doesn't trigger the component's parameter change lifecycle.
### Describe the solution you'd like
Add an option to skip navigation and only update the URL/query parameters when navigating to the same page, if it has the `[Authorize]` attribute, or make the framework's internal authorization routing cache pages if it's going to navigate to the same page.
### Additional context
_No response_
Contributor guide
Research direction
Reproduce the behavior using a page with [Authorized(Policy="...")], a custom async policy, and navigation.NavigateTo with navigation.GetUriWithQueryParameter. Compare the component lifecycle, especially OnInitialized{Async}, when only query parameters change, and review the AuthorizeView and history.PushState workarounds. Done means same-page authorized navigation preserves the component and state while still applying parameter changes.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- authentication, frontend
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100