From 8e5726116703d78331c00e53d014b6bd087bfe7c Mon Sep 17 00:00:00 2001 From: Steve Sanderson Date: Fri, 23 Feb 2018 11:05:29 +0000 Subject: [PATCH] In BrowserUriHelper, change URI caching logic to be more defensive in case OnLocationChanged isn't being used --- .../Services/BrowserUriHelper.cs | 25 ++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/src/Microsoft.AspNetCore.Blazor.Browser/Services/BrowserUriHelper.cs b/src/Microsoft.AspNetCore.Blazor.Browser/Services/BrowserUriHelper.cs index 799c6c8065..28cf8e497f 100644 --- a/src/Microsoft.AspNetCore.Blazor.Browser/Services/BrowserUriHelper.cs +++ b/src/Microsoft.AspNetCore.Blazor.Browser/Services/BrowserUriHelper.cs @@ -20,7 +20,7 @@ namespace Microsoft.AspNetCore.Blazor.Browser.Services // safety concerns due to the static state. static readonly string _functionPrefix = typeof(BrowserUriHelper).FullName; static bool _hasEnabledNavigationInterception; - static string _currentAbsoluteUri; + static string _cachedAbsoluteUri; static EventHandler _onLocationChanged; static string _baseUriString; static Uri _baseUri; @@ -37,6 +37,8 @@ namespace Microsoft.AspNetCore.Blazor.Browser.Services { // We could consider deactivating the JS-side enableNavigationInteception // if there are no remaining listeners, but we don't need that currently. + // If we ever do that, will also need to change the logic inside GetAbsoluteUri + // so it knows not to continue using the cached URI. _onLocationChanged -= value; } } @@ -51,13 +53,24 @@ namespace Microsoft.AspNetCore.Blazor.Browser.Services /// public string GetAbsoluteUri() { - if (_currentAbsoluteUri == null) + if (_cachedAbsoluteUri == null) { - _currentAbsoluteUri = RegisteredFunction.InvokeUnmarshalled( + var newUri = RegisteredFunction.InvokeUnmarshalled( $"{_functionPrefix}.getLocationHref"); - } - return _currentAbsoluteUri; + if (_hasEnabledNavigationInterception) + { + // Once we turn on navigation interception, we no longer have to query + // the browser for its URI each time (because we'd know if it had changed) + _cachedAbsoluteUri = newUri; + } + + return newUri; + } + else + { + return _cachedAbsoluteUri; + } } /// @@ -105,7 +118,7 @@ namespace Microsoft.AspNetCore.Blazor.Browser.Services private static void NotifyLocationChanged(string newAbsoluteUri) { - _currentAbsoluteUri = newAbsoluteUri; + _cachedAbsoluteUri = newAbsoluteUri; _onLocationChanged?.Invoke(null, newAbsoluteUri); }