Skip to content

Commit fe9f08a

Browse files
committed
Update enhancednavigationstart event
1 parent cd3bcf7 commit fe9f08a

File tree

5 files changed

+19
-17
lines changed

5 files changed

+19
-17
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ function boot(options?: Partial<WebStartOptions>) : Promise<void> {
5151
const jsEventRegistry = JSEventRegistry.create(Blazor);
5252

5353
const navigationEnhancementCallbacks: NavigationEnhancementCallbacks = {
54-
enhancedNavigationStarted: (method) => {
55-
jsEventRegistry.dispatchEvent('enhancednavigationstart', { method });
54+
enhancedNavigationStarted: (resource, options) => {
55+
jsEventRegistry.dispatchEvent('enhancednavigationstart', { resource, options });
5656
},
5757
documentUpdated: () => {
5858
rootComponentManager.onDocumentUpdated();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function afterInitialPageLoad() {
3232

3333
function onEnhancedNavigationStart(ev: EnhancedNavigationStartEvent) {
3434
// Only focus on enhanced load if the enhanced navigation is not a form post.
35-
allowFocusOnEnhancedLoad = ev.method !== 'post';
35+
allowFocusOnEnhancedLoad = ev.options.method !== 'post';
3636
}
3737

3838
function onEnhancedLoad() {

src/Components/Web.JS/src/Services/JSEventRegistry.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ interface BlazorEvent {
1010
}
1111

1212
export interface EnhancedNavigationStartEvent extends BlazorEvent {
13-
method: string;
13+
resource: string | URL | Request;
14+
options: RequestInit;
1415
}
1516

1617
// Maps Blazor event names to the argument type passed to registered listeners.

src/Components/Web.JS/src/Services/NavigationEnhancement.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
import { synchronizeDomContent } from '../Rendering/DomMerging/DomSync';
5-
import { attachProgrammaticEnhancedNavigationHandler, handleClickForNavigationInterception, hasInteractiveRouter, isSamePageWithHash, notifyEnhancedNavigationListners, performScrollToElementOnTheSamePage } from './NavigationUtils';
5+
import { attachProgrammaticEnhancedNavigationHandler, handleClickForNavigationInterception, hasInteractiveRouter, isSamePageWithHash, notifyEnhancedNavigationListeners, performScrollToElementOnTheSamePage } from './NavigationUtils';
66

77
/*
88
In effect, we have two separate client-side navigation mechanisms:
@@ -42,7 +42,7 @@ let performingEnhancedPageLoad: boolean;
4242
let currentContentUrl = location.href;
4343

4444
export interface NavigationEnhancementCallbacks {
45-
enhancedNavigationStarted: (method: string) => void;
45+
enhancedNavigationStarted: (resource: string | URL | Request, options: RequestInit) => void;
4646
documentUpdated: () => void;
4747
enhancedNavigationCompleted: () => void;
4848
}
@@ -184,29 +184,30 @@ export async function performEnhancedPageLoad(internalDestinationHref: string, i
184184
currentEnhancedNavigationAbortController?.abort();
185185

186186
// Notify any interactive runtimes that an enhanced navigation is starting
187-
notifyEnhancedNavigationListners(internalDestinationHref, interceptedLink);
188-
189-
// Invoke other enhanced navigation handlers
190-
const requestMethod = fetchOptions?.method ?? 'get';
191-
navigationEnhancementCallbacks.enhancedNavigationStarted(requestMethod);
187+
notifyEnhancedNavigationListeners(internalDestinationHref, interceptedLink);
192188

193189
// Now request the new page via fetch, and a special header that tells the server we want it to inject
194190
// framing boundaries to distinguish the initial document and each subsequent streaming SSR update.
195191
currentEnhancedNavigationAbortController = new AbortController();
196192
const abortSignal = currentEnhancedNavigationAbortController.signal;
197-
const responsePromise = fetch(internalDestinationHref, Object.assign(<RequestInit>{
193+
const requestInit = Object.assign(<RequestInit>{
198194
signal: abortSignal,
199195
mode: 'no-cors', // If there's a redirection to an external origin, even if it enables CORS, we don't want to receive its content and patch it into our DOM on this origin
200196
headers: {
201197
// Because of no-cors, we can only send CORS-safelisted headers, so communicate the info about
202198
// enhanced nav as a MIME type parameter
203199
'accept': acceptHeader,
204200
},
205-
}, fetchOptions));
201+
}, fetchOptions);
202+
const responsePromise = fetch(internalDestinationHref, requestInit);
203+
204+
// Notify handlers that enhanced navigation has started
205+
navigationEnhancementCallbacks.enhancedNavigationStarted(internalDestinationHref, requestInit);
206+
206207
let isNonRedirectedPostToADifferentUrlMessage: string | null = null;
207208
await getResponsePartsWithFraming(responsePromise, abortSignal,
208209
(response, initialContent) => {
209-
const isGetRequest = requestMethod === 'get';
210+
const isGetRequest = !fetchOptions?.method || fetchOptions.method === 'get';
210211
const isSuccessResponse = response.status >= 200 && response.status < 300;
211212

212213
// For true 301/302/etc redirections to external URLs, we'll receive an opaque response
@@ -306,7 +307,7 @@ export async function performEnhancedPageLoad(internalDestinationHref: string, i
306307
retryEnhancedNavAsFullPageLoad(internalDestinationHref);
307308
} else {
308309
// For non-get requests, we can't safely re-request, so just treat it as an error
309-
replaceDocumentWithPlainText(`Error: ${requestMethod} request to ${internalDestinationHref} returned non-HTML content of type ${responseContentType || 'unspecified'}.`);
310+
replaceDocumentWithPlainText(`Error: ${fetchOptions.method} request to ${internalDestinationHref} returned non-HTML content of type ${responseContentType || 'unspecified'}.`);
310311
}
311312
}
312313
},

src/Components/Web.JS/src/Services/NavigationUtils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { WebRendererId } from '../Rendering/WebRendererId';
55

66
let interactiveRouterRendererId: WebRendererId | undefined = undefined;
77
let programmaticEnhancedNavigationHandler: typeof performProgrammaticEnhancedNavigation | undefined;
8-
let enhancedNavigationListener: typeof notifyEnhancedNavigationListners | undefined;
8+
let enhancedNavigationListener: typeof notifyEnhancedNavigationListeners | undefined;
99

1010
/**
1111
* Checks if a click event corresponds to an <a> tag referencing a URL within the base href, and that interception
@@ -70,7 +70,7 @@ export function attachEnhancedNavigationListener(listener: typeof enhancedNaviga
7070
enhancedNavigationListener = listener;
7171
}
7272

73-
export function notifyEnhancedNavigationListners(internalDestinationHref: string, interceptedLink: boolean) {
73+
export function notifyEnhancedNavigationListeners(internalDestinationHref: string, interceptedLink: boolean) {
7474
enhancedNavigationListener?.(internalDestinationHref, interceptedLink);
7575
}
7676

0 commit comments

Comments
 (0)