Overload UriHelper to forceLoad the page (imported from Blazor PR 1154) (#4786)

This commit is contained in:
Steve Sanderson 2018-12-14 17:05:45 +00:00 committed by GitHub
parent 8fbb9fb0e6
commit 93127b39e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 61 additions and 16 deletions

View File

@ -41,9 +41,10 @@ function enableNavigationInterception(assemblyName: string, functionName: string
window.addEventListener('popstate', handleInternalNavigation); window.addEventListener('popstate', handleInternalNavigation);
} }
export function navigateTo(uri: string) { export function navigateTo(uri: string, forceLoad: boolean) {
const absoluteUri = toAbsoluteUri(uri); const absoluteUri = toAbsoluteUri(uri);
if (isWithinBaseUriSpace(absoluteUri)) {
if (!forceLoad && isWithinBaseUriSpace(absoluteUri)) {
performInternalNavigation(absoluteUri); performInternalNavigation(absoluteUri);
} else { } else {
location.href = uri; location.href = uri;

View File

@ -47,14 +47,14 @@ namespace Microsoft.AspNetCore.Components.Browser.Services
} }
/// <inheritdoc /> /// <inheritdoc />
protected override void NavigateToCore(string uri) protected override void NavigateToCore(string uri, bool forceLoad)
{ {
if (uri == null) if (uri == null)
{ {
throw new ArgumentNullException(nameof(uri)); throw new ArgumentNullException(nameof(uri));
} }
((IJSInProcessRuntime)JSRuntime.Current).Invoke<object>(Interop.NavigateTo, uri); ((IJSInProcessRuntime)JSRuntime.Current).Invoke<object>(Interop.NavigateTo, uri, forceLoad);
} }
/// <summary> /// <summary>

View File

@ -61,14 +61,10 @@ namespace Microsoft.AspNetCore.Components.Server.Circuits
uriHelper.TriggerOnLocationChanged(); uriHelper.TriggerOnLocationChanged();
} }
/// <summary> /// <inheritdoc />
/// Navigates to the specified URI. protected override void NavigateToCore(string uri, bool forceLoad)
/// </summary>
/// <param name="uri">The destination URI. This can be absolute, or relative to the base URI
/// (as returned by <see cref="IUriHelper.GetBaseUri"/>).</param>
protected override void NavigateToCore(string uri)
{ {
_jsRuntime.InvokeAsync<object>(Interop.NavigateTo, uri); _jsRuntime.InvokeAsync<object>(Interop.NavigateTo, uri, forceLoad);
} }
} }
} }

View File

@ -51,5 +51,13 @@ namespace Microsoft.AspNetCore.Components.Services
/// <param name="uri">The destination URI. This can be absolute, or relative to the base URI /// <param name="uri">The destination URI. This can be absolute, or relative to the base URI
/// (as returned by <see cref="GetBaseUri"/>).</param> /// (as returned by <see cref="GetBaseUri"/>).</param>
void NavigateTo(string uri); void NavigateTo(string uri);
/// <summary>
/// Navigates to the specified URI.
/// </summary>
/// <param name="uri">The destination URI. This can be absolute, or relative to the base URI
/// (as returned by <see cref="GetBaseUri"/>).</param>
/// <param name="forceLoad">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.</param>
void NavigateTo(string uri, bool forceLoad);
} }
} }

View File

@ -46,8 +46,7 @@ namespace Microsoft.AspNetCore.Components.Services
/// (as returned by <see cref="GetBaseUri"/>).</param> /// (as returned by <see cref="GetBaseUri"/>).</param>
public void NavigateTo(string uri) public void NavigateTo(string uri)
{ {
EnsureInitialized(); NavigateTo(uri, forceLoad: false);
NavigateToCore(uri);
} }
/// <summary> /// <summary>
@ -55,7 +54,20 @@ namespace Microsoft.AspNetCore.Components.Services
/// </summary> /// </summary>
/// <param name="uri">The destination URI. This can be absolute, or relative to the base URI /// <param name="uri">The destination URI. This can be absolute, or relative to the base URI
/// (as returned by <see cref="GetBaseUri"/>).</param> /// (as returned by <see cref="GetBaseUri"/>).</param>
protected abstract void NavigateToCore(string uri); /// <param name="forceLoad">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.</param>
public void NavigateTo(string uri, bool forceLoad)
{
EnsureInitialized();
NavigateToCore(uri, forceLoad);
}
/// <summary>
/// Navigates to the specified URI.
/// </summary>
/// <param name="uri">The destination URI. This can be absolute, or relative to the base URI
/// (as returned by <see cref="GetBaseUri"/>).</param>
/// <param name="forceLoad">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.</param>
protected abstract void NavigateToCore(string uri, bool forceLoad);
/// <summary> /// <summary>
/// Called to initialize BaseURI and current URI before those values the first time. /// Called to initialize BaseURI and current URI before those values the first time.

View File

@ -268,9 +268,33 @@ namespace Microsoft.AspNetCore.Components.E2ETest.Tests
SetUrlViaPushState("/"); SetUrlViaPushState("/");
var app = MountTestComponent<TestRouter>(); var app = MountTestComponent<TestRouter>();
app.FindElement(By.TagName("button")).Click(); var testSelector = WaitUntilTestSelectorReady();
app.FindElement(By.Id("do-navigation")).Click();
WaitAssert.True(() => Browser.Url.EndsWith("/Other"));
WaitAssert.Equal("This is another page.", () => app.FindElement(By.Id("test-info")).Text); WaitAssert.Equal("This is another page.", () => app.FindElement(By.Id("test-info")).Text);
AssertHighlightedLinks("Other", "Other with base-relative URL (matches all)"); AssertHighlightedLinks("Other", "Other with base-relative URL (matches all)");
// Because this was client-side navigation, we didn't lose the state in the test selector
Assert.Equal(typeof(TestRouter).FullName, testSelector.SelectedOption.GetAttribute("value"));
}
[Fact]
public void CanNavigateProgrammaticallyWithForceLoad()
{
SetUrlViaPushState("/");
var app = MountTestComponent<TestRouter>();
var testSelector = WaitUntilTestSelectorReady();
app.FindElement(By.Id("do-navigation-forced")).Click();
WaitAssert.True(() => Browser.Url.EndsWith("/Other"));
// Because this was a full-page load, our element references should no longer be valid
Assert.Throws<StaleElementReferenceException>(() =>
{
testSelector.SelectedOption.GetAttribute("value");
});
} }
[Fact] [Fact]

View File

@ -13,10 +13,14 @@
<li><NavLink href="/subdir/WithParameters/Name/Abc/LastName/McDef">With parameters</NavLink></li> <li><NavLink href="/subdir/WithParameters/Name/Abc/LastName/McDef">With parameters</NavLink></li>
</ul> </ul>
<button onclick=@(x => uriHelper.NavigateTo("Other"))> <button id="do-navigation" onclick=@(x => uriHelper.NavigateTo("Other"))>
Programmatic navigation Programmatic navigation
</button> </button>
<button id="do-navigation-forced" onclick=@(x => uriHelper.NavigateTo("Other", true))>
Programmatic navigation with force-load
</button>
<a id="anchor-with-no-href"> <a id="anchor-with-no-href">
Anchor tag with no href attribute Anchor tag with no href attribute
</a> </a>