Skip to content

Commit 303c6f9

Browse files
[release/8.0] [Blazor] each navigation to external logs 2 errors a minute later (#53297)
Backport of #53271 to release/8.0 /cc @javiercn # [Blazor] each navigation to external logs 2 errors a minute later Capture an exception and log a message when navigation fails because the session has ended. ## Description - The user clicks the link to the target URL. - That link is intercepted in the JavaScript. - .NET is called to determine if navigation to the URL needs to be prevented. - It is determined that there is no need to prevent navigation and "navigateTo" is called via JS interop in NavigationManager.ts. - The URL is determined to be external by "navigateTo" and the location is set to the new URL. - The unload event in the browser is automatically triggered, which in turn triggers the call to _disconnect. - As a result, the promise to "navigateTo" never completes (the page gets unloaded before that) and the associated task in .NET ends up getting cancelled with a timeout. Fixes #45267 ## Customer Impact Customers see a large number of errors in their logs when their sites contain links to external sites, which in turn can trigger alerts in their incident monitoring systems. ## Regression? - [ ] Yes - [X] No ## Risk - [ ] High - [ ] Medium - [X] Low The scenario in which the issue happens is well defined and the fix is simple. ## Verification - [X] Manual (required) - [ ] Automated ![image](https://github.com/dotnet/aspnetcore/assets/6995051/a61a9898-771a-414c-a83c-7836bf88697c) ## Packaging changes reviewed? - [ ] Yes - [ ] No - [X] N/A ---- ## When servicing release/2.1 - [ ] Make necessary changes in eng/PatchConfig.props --------- Co-authored-by: jacalvar <[email protected]>
1 parent e74a5cf commit 303c6f9

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

src/Components/Server/src/Circuits/RemoteJSRuntime.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ internal partial class RemoteJSRuntime : JSRuntime
3030

3131
public bool IsInitialized => _clientProxy is not null;
3232

33+
internal bool IsPermanentlyDisconnected => _permanentlyDisconnected;
34+
3335
/// <summary>
3436
/// Notifies when a runtime exception occurred.
3537
/// </summary>

src/Components/Server/src/Circuits/RemoteNavigationManager.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ async Task PerformNavigationAsync()
106106
}
107107

108108
await _jsRuntime.InvokeVoidAsync(Interop.NavigateTo, uri, options);
109+
Log.NavigationCompleted(_logger, uri);
110+
}
111+
catch (TaskCanceledException)
112+
when (_jsRuntime is RemoteJSRuntime remoteRuntime && remoteRuntime.IsPermanentlyDisconnected)
113+
{
114+
Log.NavigationStoppedSessionEnded(_logger, uri);
109115
}
110116
catch (Exception ex)
111117
{
@@ -190,5 +196,11 @@ public static void RequestingNavigation(ILogger logger, string uri, NavigationOp
190196

191197
[LoggerMessage(5, LogLevel.Error, "Failed to refresh", EventName = "RefreshFailed")]
192198
public static partial void RefreshFailed(ILogger logger, Exception exception);
199+
200+
[LoggerMessage(6, LogLevel.Debug, "Navigation completed when changing the location to {Uri}", EventName = "NavigationCompleted")]
201+
public static partial void NavigationCompleted(ILogger logger, string uri);
202+
203+
[LoggerMessage(7, LogLevel.Debug, "Navigation stopped because the session ended when navigating to {Uri}", EventName = "NavigationStoppedSessionEnded")]
204+
public static partial void NavigationStoppedSessionEnded(ILogger logger, string uri);
193205
}
194206
}

0 commit comments

Comments
 (0)