Skip to content

Commit 844e28b

Browse files
[release/8.0] [Blazor] Fix for URLs/querystrings not updating for interactive components when using SSR routing (#51279)
# Invoke `LocationChanged` for interactive components when an enhanced navigation occurs This PR makes the `LocationChanged` event get invoked for interactive runtimes when an enhanced navigation occurs. ## Description This PR makes the following changes: * The `LocationChanged` event gets invoked for all interactive runtimes when either a client-side or enhanced navigation occurs * Previously: * An enhanced navigation would have not invoked the `LocationChanged` event in interactive runtimes * A client-side navigation would only invoke the `LocationChanged` event for the runtime that started most recently * Attempting to enable interactive routing from multiple interactive runtimes results in an error at runtime * If an interactive router is present but multiple runtimes are available, the `LocationChanging` event only gets invoked for the runtime that has the interactive router * It would take additional work to allow either runtime to cancel a client-side navigation, and that's outside the scope of this PR. We have #48766 to track doing the extra work to support this scenario Fixes #51216 ## Customer Impact Without this changes the customers will encounter problems with UI being correctly updated during enhanced navigation because `LocationChanging` event not firing. Besides the UI not updating correctly this will also cause customer registered handlers for the `LocationChanged` event not firing resulting in unexpected behaviors. ## Regression? - [ ] Yes - [x] No [If yes, specify the version the behavior has regressed from] ## Risk - [ ] High - [ ] Medium - [x] Low Despite the size of this change, we have quite extensive automated tests (including these newly added ones) around the interactive routing scenarios, so we expect that nothing will break from this change. ## Verification - [x] Manual (required) - [x] Automated ## Packaging changes reviewed? - [ ] Yes - [x] No - [ ] N/A ---- ## When servicing release/2.1 - [ ] Make necessary changes in eng/PatchConfig.props --------- Co-authored-by: Surayya Huseyn Zada <[email protected]> Co-authored-by: Surayya Huseyn Zada <[email protected]>
1 parent eec5dc1 commit 844e28b

22 files changed

+354
-62
lines changed

src/Components/Server/src/Circuits/RemoteNavigationInterception.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@ public async Task EnableNavigationInterceptionAsync()
3535
"attempted during prerendering or while the client is disconnected.");
3636
}
3737

38-
await _jsRuntime.InvokeAsync<object>(Interop.EnableNavigationInterception);
38+
await _jsRuntime.InvokeAsync<object>(Interop.EnableNavigationInterception, WebRendererId.Server);
3939
}
4040
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ private async Task SetHasLocationChangingListenersAsync(bool value)
157157
{
158158
try
159159
{
160-
await _jsRuntime.InvokeVoidAsync(Interop.SetHasLocationChangingListeners, value);
160+
await _jsRuntime.InvokeVoidAsync(Interop.SetHasLocationChangingListeners, WebRendererId.Server, value);
161161
}
162162
catch (JSDisconnectedException)
163163
{

src/Components/Web.JS/dist/Release/blazor.server.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Components/Web.JS/dist/Release/blazor.web.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Components/Web.JS/dist/Release/blazor.webview.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Components/Web.JS/src/Boot.Server.Common.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { DefaultReconnectionHandler } from './Platform/Circuits/DefaultReconnect
1010
import { discoverServerPersistedState, ServerComponentDescriptor } from './Services/ComponentDescriptorDiscovery';
1111
import { fetchAndInvokeInitializers } from './JSInitializers/JSInitializers.Server';
1212
import { RootComponentManager } from './Services/RootComponentManager';
13+
import { WebRendererId } from './Rendering/WebRendererId';
1314

1415
let initializersPromise: Promise<void> | undefined;
1516
let appState: string;
@@ -74,7 +75,7 @@ async function startServerCore(components: RootComponentManager<ServerComponentD
7475
options.reconnectionHandler = options.reconnectionHandler || Blazor.defaultReconnectionHandler;
7576

7677
// Configure navigation via SignalR
77-
Blazor._internal.navigationManager.listenForNavigationEvents((uri: string, state: string | undefined, intercepted: boolean): Promise<void> => {
78+
Blazor._internal.navigationManager.listenForNavigationEvents(WebRendererId.Server, (uri: string, state: string | undefined, intercepted: boolean): Promise<void> => {
7879
return circuit.sendLocationChanged(uri, state, intercepted);
7980
}, (callId: number, uri: string, state: string | undefined, intercepted: boolean): Promise<void> => {
8081
return circuit.sendLocationChanging(callId, uri, state, intercepted);

src/Components/Web.JS/src/Boot.WebAssembly.Common.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { receiveDotNetDataStream } from './StreamingInterop';
1616
import { WebAssemblyComponentAttacher } from './Platform/WebAssemblyComponentAttacher';
1717
import { MonoConfig } from 'dotnet';
1818
import { RootComponentManager } from './Services/RootComponentManager';
19+
import { WebRendererId } from './Rendering/WebRendererId';
1920

2021
let options: Partial<WebAssemblyStartOptions> | undefined;
2122
let initializersPromise: Promise<void>;
@@ -127,7 +128,7 @@ async function startCore(components: RootComponentManager<WebAssemblyComponentDe
127128
}
128129
};
129130

130-
Blazor._internal.navigationManager.listenForNavigationEvents(async (uri: string, state: string | undefined, intercepted: boolean): Promise<void> => {
131+
Blazor._internal.navigationManager.listenForNavigationEvents(WebRendererId.WebAssembly, async (uri: string, state: string | undefined, intercepted: boolean): Promise<void> => {
131132
await dispatcher.invokeDotNetStaticMethodAsync(
132133
'Microsoft.AspNetCore.Components.WebAssembly',
133134
'NotifyLocationChanged',

src/Components/Web.JS/src/Boot.WebView.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { startIpcReceiver } from './Platform/WebView/WebViewIpcReceiver';
99
import { sendAttachPage, sendBeginInvokeDotNetFromJS, sendEndInvokeJSFromDotNet, sendByteArray, sendLocationChanged, sendLocationChanging } from './Platform/WebView/WebViewIpcSender';
1010
import { fetchAndInvokeInitializers } from './JSInitializers/JSInitializers.WebView';
1111
import { receiveDotNetDataStream } from './StreamingInterop';
12+
import { WebRendererId } from './Rendering/WebRendererId';
1213

1314
let started = false;
1415

@@ -32,8 +33,8 @@ async function boot(): Promise<void> {
3233

3334
Blazor._internal.receiveWebViewDotNetDataStream = receiveWebViewDotNetDataStream;
3435

35-
navigationManagerFunctions.enableNavigationInterception();
36-
navigationManagerFunctions.listenForNavigationEvents(sendLocationChanged, sendLocationChanging);
36+
navigationManagerFunctions.enableNavigationInterception(WebRendererId.WebView);
37+
navigationManagerFunctions.listenForNavigationEvents(WebRendererId.WebView, sendLocationChanged, sendLocationChanging);
3738

3839
sendAttachPage(navigationManagerFunctions.getBaseURI(), navigationManagerFunctions.getLocationHref());
3940
await jsInitializer.invokeAfterStartedCallbacks(Blazor);

src/Components/Web.JS/src/Platform/WebView/WebViewIpcReceiver.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ export function startIpcReceiver(): void {
4343

4444
'Refresh': navigationManagerFunctions.refresh,
4545

46-
'SetHasLocationChangingListeners': navigationManagerFunctions.setHasLocationChangingListeners,
46+
'SetHasLocationChangingListeners': (hasListeners: boolean) => {
47+
navigationManagerFunctions.setHasLocationChangingListeners(WebRendererId.WebView, hasListeners);
48+
},
4749

4850
'EndLocationChanging': navigationManagerFunctions.endLocationChanging,
4951
};

src/Components/Web.JS/src/Rendering/StreamingRendering.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class BlazorStreamingUpdate extends HTMLElement {
6060
// The URL was already updated on the original link click. Replace so that 'back' goes to the pre-redirection location.
6161
history.replaceState(null, '', destinationUrl);
6262
}
63-
performEnhancedPageLoad(destinationUrl);
63+
performEnhancedPageLoad(destinationUrl, /* interceptedLink */ false);
6464
} else {
6565
// Same reason for varying as above
6666
if (isFormPost) {

0 commit comments

Comments
 (0)