Skip to content
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

Add NavigationManager.OnLocationChanging (#14962) #16607

Closed
wants to merge 2 commits into from
Closed

Add NavigationManager.OnLocationChanging (#14962) #16607

wants to merge 2 commits into from

Conversation

mrpmorris
Copy link

  • Added OnLocationChanging event to router
  • Allow cancelling of navigation via args.PreventNavigation()

Addresses #14962

@Pilchie Pilchie added the area-blazor Includes: Blazor, Razor Components label Oct 28, 2019
@SteveSandersonMS
Copy link
Member

Thanks for suggesting this, @mrpmorris.

Currently I don't quite follow what the intended design is here. This only runs CanNavigateTo when NavigateToCore is about to execute, which only happens when the developer triggers navigation programmatically using NavigationManager.NavigateTo. It will not have any effect on navigation triggered by clicking on <a> tags. As such it doesn't seem very useful - if a developer really wants to run special logic before programmatic navigation, they can just put that in their code directly before their call to NavigationManager.NavigateTo. If they really want to centralise this check in one place, they can create their own utility method that runs their own CanNavigateTo before calling NavigationManager.NavigateTo, so it doesn't need to be baked into NavigationManager itself.

I'm guessing it's intentional to only do this for programmatic navigation, since it wouldn't be possible to block navigation that occurs when people click on <a> tags. For Blazor Server, the logic has to run asynchronously so the JS event can't be cancelled. Event for Blazor WebAssembly or any JS-based SPA framework, it doesn't make sense to block clicks on <a> tags, since browsers won't always honour your intentions (e.g., if a user right-clicks and chooses Open in new tab).

@mrpmorris
Copy link
Author

Sorry, I must have misunderstood. I thought the JS intercepted navigations and then pushed them through to Blazor.

If that's not the case then this PR is quite useless. I found it quite difficult to test the changes, so thanks for having a look!

@mrpmorris mrpmorris closed this Oct 29, 2019
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.

4 participants