-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Navigate with "replace" param #33751
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
Changes from all commits
86cbf05
253481f
dec6753
4434740
71e9a94
fc21398
6f9432f
63fecca
b4fb11c
6ece74b
097ebe2
0414f1b
7687790
42b8e0d
0e5342f
2d64856
92cd5b8
983c10a
4fb201b
a68395e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
namespace Microsoft.AspNetCore.Components | ||
{ | ||
/// <summary> | ||
/// Additional options for navigating to another URI. | ||
/// </summary> | ||
public readonly struct NavigationOptions | ||
{ | ||
/// <summary> | ||
/// If true, bypasses client-side routing and forces the browser to load the new page from the server, whether or not the URI would normally be handled by the client-side router. | ||
/// </summary> | ||
public bool ForceLoad { get; init; } | ||
|
||
/// <summary> | ||
/// If true, replaces the currently entry in the history stack. | ||
/// If false, appends the new entry to the history stack. | ||
/// </summary> | ||
public bool ReplaceHistoryEntry { get; init; } | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,8 @@ | |
*REMOVED*static Microsoft.AspNetCore.Components.ParameterView.FromDictionary(System.Collections.Generic.IDictionary<string!, object!>! parameters) -> Microsoft.AspNetCore.Components.ParameterView | ||
*REMOVED*virtual Microsoft.AspNetCore.Components.RenderTree.Renderer.DispatchEventAsync(ulong eventHandlerId, Microsoft.AspNetCore.Components.RenderTree.EventFieldInfo! fieldInfo, System.EventArgs! eventArgs) -> System.Threading.Tasks.Task! | ||
*REMOVED*readonly Microsoft.AspNetCore.Components.RenderTree.RenderTreeEdit.RemovedAttributeName -> string! | ||
*REMOVED*Microsoft.AspNetCore.Components.NavigationManager.NavigateTo(string! uri, bool forceLoad = false) -> void | ||
*REMOVED*abstract Microsoft.AspNetCore.Components.NavigationManager.NavigateToCore(string! uri, bool forceLoad) -> void | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Although these show as removed, there should be no back-compat loss (source or binary) because of adding new equivalents. I think our PublicAPI tooling doesn't reflect some details about how adding new optional params. But if you're code-reviewing this, please check you agree with my claim here :) |
||
Microsoft.AspNetCore.Components.ComponentApplicationState | ||
Microsoft.AspNetCore.Components.ComponentApplicationState.OnPersisting -> Microsoft.AspNetCore.Components.ComponentApplicationState.OnPersistingCallback! | ||
Microsoft.AspNetCore.Components.ComponentApplicationState.OnPersistingCallback | ||
|
@@ -29,6 +31,15 @@ Microsoft.AspNetCore.Components.Lifetime.ComponentApplicationLifetime.State.get | |
Microsoft.AspNetCore.Components.Lifetime.IComponentApplicationStateStore | ||
Microsoft.AspNetCore.Components.Lifetime.IComponentApplicationStateStore.GetPersistedStateAsync() -> System.Threading.Tasks.Task<System.Collections.Generic.IDictionary<string!, byte[]!>!>! | ||
Microsoft.AspNetCore.Components.Lifetime.IComponentApplicationStateStore.PersistStateAsync(System.Collections.Generic.IReadOnlyDictionary<string!, byte[]!>! state) -> System.Threading.Tasks.Task! | ||
Microsoft.AspNetCore.Components.NavigationManager.NavigateTo(string! uri, Microsoft.AspNetCore.Components.NavigationOptions options) -> void | ||
Microsoft.AspNetCore.Components.NavigationManager.NavigateTo(string! uri, bool forceLoad = false, bool replace = false) -> void | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need this overload? I think we should force people into using the navigation options directly and set ourselves for success in the future. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Personally I find: navManager.NavigateTo("something", replace: true); to be a lot more readable than: navManager.NavigateTo("something", new NavigationOptions { ReplaceHistoryEntry = true }); However I understand it's always a subjective balance when we consider adding overloads just for convenience. It depends on how universal we think the convenience-overload pattern will be, and whether it will stand the test of time as being the only extra convenience we need, vs something that ends up getting multiplied out into many other overload variants. (I know you know this; just giving context for anyone else reading) We do need at least to keep the Shall we leave a final call on this until we get to API review, or do you feel strongly now? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't feel strongly about it, it was mainly a suggestion. |
||
Microsoft.AspNetCore.Components.NavigationManager.NavigateTo(string! uri, bool forceLoad) -> void | ||
Microsoft.AspNetCore.Components.NavigationOptions | ||
Microsoft.AspNetCore.Components.NavigationOptions.ForceLoad.get -> bool | ||
Microsoft.AspNetCore.Components.NavigationOptions.ForceLoad.init -> void | ||
Microsoft.AspNetCore.Components.NavigationOptions.NavigationOptions() -> void | ||
Microsoft.AspNetCore.Components.NavigationOptions.ReplaceHistoryEntry.get -> bool | ||
Microsoft.AspNetCore.Components.NavigationOptions.ReplaceHistoryEntry.init -> void | ||
Microsoft.AspNetCore.Components.RenderHandle.IsHotReloading.get -> bool | ||
Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder.Dispose() -> void | ||
Microsoft.AspNetCore.Components.Routing.Router.PreferExactMatches.get -> bool | ||
|
@@ -49,6 +60,8 @@ Microsoft.AspNetCore.Components.RenderTree.Renderer.GetEventArgsType(ulong event | |
abstract Microsoft.AspNetCore.Components.ErrorBoundaryBase.OnErrorAsync(System.Exception! exception) -> System.Threading.Tasks.Task! | ||
override Microsoft.AspNetCore.Components.LayoutComponentBase.SetParametersAsync(Microsoft.AspNetCore.Components.ParameterView parameters) -> System.Threading.Tasks.Task! | ||
static Microsoft.AspNetCore.Components.ParameterView.FromDictionary(System.Collections.Generic.IDictionary<string!, object?>! parameters) -> Microsoft.AspNetCore.Components.ParameterView | ||
virtual Microsoft.AspNetCore.Components.NavigationManager.NavigateToCore(string! uri, Microsoft.AspNetCore.Components.NavigationOptions options) -> void | ||
virtual Microsoft.AspNetCore.Components.NavigationManager.NavigateToCore(string! uri, bool forceLoad) -> void | ||
virtual Microsoft.AspNetCore.Components.RenderTree.Renderer.DispatchEventAsync(ulong eventHandlerId, Microsoft.AspNetCore.Components.RenderTree.EventFieldInfo? fieldInfo, System.EventArgs! eventArgs) -> System.Threading.Tasks.Task! | ||
readonly Microsoft.AspNetCore.Components.RenderTree.RenderTreeEdit.RemovedAttributeName -> string? | ||
*REMOVED*Microsoft.AspNetCore.Components.CascadingValue<TValue>.Value.get -> TValue | ||
|
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,46 +60,64 @@ export function attachToEventDelegator(eventDelegator: EventDelegator) { | |
|
||
if (isWithinBaseUriSpace(absoluteHref)) { | ||
event.preventDefault(); | ||
performInternalNavigation(absoluteHref, true); | ||
performInternalNavigation(absoluteHref, /* interceptedLink */ true, /* replace */ false); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
export function navigateTo(uri: string, forceLoad: boolean, replace: boolean = false) { | ||
// For back-compat, we need to accept multiple overloads | ||
export function navigateTo(uri: string, options: NavigationOptions): void; | ||
export function navigateTo(uri: string, forceLoad: boolean): void; | ||
export function navigateTo(uri: string, forceLoad: boolean, replace: boolean): void; | ||
export function navigateTo(uri: string, forceLoadOrOptions: NavigationOptions | boolean, replaceIfUsingOldOverload: boolean = false) { | ||
Comment on lines
+69
to
+73
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I haven't seen this used in a while in TS 😄. I believe There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's actually not within internal, however it's here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I saw that later. If you want to update the auth package as part of this change to rely on that, It would be nice. |
||
const absoluteUri = toAbsoluteUri(uri); | ||
|
||
if (!forceLoad && isWithinBaseUriSpace(absoluteUri)) { | ||
// It's an internal URL, so do client-side navigation | ||
performInternalNavigation(absoluteUri, false, replace); | ||
} else if (forceLoad && location.href === uri) { | ||
// Force-loading the same URL you're already on requires special handling to avoid | ||
// triggering browser-specific behavior issues. | ||
// Normalize the parameters to the newer overload (i.e., using NavigationOptions) | ||
const options: NavigationOptions = forceLoadOrOptions instanceof Object | ||
? forceLoadOrOptions | ||
: { forceLoad: forceLoadOrOptions, replaceHistoryEntry: replaceIfUsingOldOverload }; | ||
|
||
if (!options.forceLoad && isWithinBaseUriSpace(absoluteUri)) { | ||
performInternalNavigation(absoluteUri, false, options.replaceHistoryEntry); | ||
} else { | ||
// For external navigation, we work in terms of the originally-supplied uri string, | ||
// not the computed absoluteUri. This is in case there are some special URI formats | ||
// we're unable to translate into absolute URIs. | ||
performExternalNavigation(uri, options.replaceHistoryEntry); | ||
} | ||
} | ||
|
||
function performExternalNavigation(uri: string, replace: boolean) { | ||
if (location.href === uri) { | ||
// If you're already on this URL, you can't append another copy of it to the history stack, | ||
// so we can ignore the 'replace' flag. However, reloading the same URL you're already on | ||
// requires special handling to avoid triggering browser-specific behavior issues. | ||
// For details about what this fixes and why, see https://github.com/dotnet/aspnetcore/pull/10839 | ||
const temporaryUri = uri + '?'; | ||
history.replaceState(null, '', temporaryUri); | ||
location.replace(uri); | ||
} else if (replace){ | ||
history.replaceState(null, '', absoluteUri) | ||
} else if (replace) { | ||
location.replace(uri); | ||
} else { | ||
// It's either an external URL, or forceLoad is requested, so do a full page load | ||
location.href = uri; | ||
} | ||
} | ||
|
||
function performInternalNavigation(absoluteInternalHref: string, interceptedLink: boolean, replace: boolean = false) { | ||
function performInternalNavigation(absoluteInternalHref: string, interceptedLink: boolean, replace: boolean) { | ||
// Since this was *not* triggered by a back/forward gesture (that goes through a different | ||
// code path starting with a popstate event), we don't want to preserve the current scroll | ||
// position, so reset it. | ||
// To avoid ugly flickering effects, we don't want to change the scroll position until the | ||
// To avoid ugly flickering effects, we don't want to change the scroll position until | ||
// we render the new page. As a best approximation, wait until the next batch. | ||
resetScrollAfterNextBatch(); | ||
|
||
if(!replace){ | ||
if (!replace) { | ||
history.pushState(null, /* ignored title */ '', absoluteInternalHref); | ||
}else{ | ||
} else { | ||
history.replaceState(null, /* ignored title */ '', absoluteInternalHref); | ||
} | ||
|
||
notifyLocationChanged(interceptedLink); | ||
} | ||
|
||
|
@@ -166,3 +184,9 @@ function canProcessAnchor(anchorTarget: HTMLAnchorElement) { | |
const opensInSameFrame = !targetAttributeValue || targetAttributeValue === '_self'; | ||
return opensInSameFrame && anchorTarget.hasAttribute('href') && !anchorTarget.hasAttribute('download'); | ||
} | ||
|
||
// Keep in sync with Components/src/NavigationOptions.cs | ||
export interface NavigationOptions { | ||
forceLoad: boolean; | ||
replaceHistoryEntry: boolean; | ||
} |
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.
In an earlier edition of this PR,
NavigationOptions
was a flags enum. I've changed it to a full-fledgedstruct
because I don't want to get into this situation again in the future where we need to add some other option and have to multiply out the number of overloads and obscure rules for what methods you have to implement in subclasses.With a struct, even if we need new options of type
string
, or rules about which combinations of options are legal to use simultaneously, it should be possible to model that (e.g., as different constructor overloads).