@@ -35,9 +35,15 @@ different bundles that only contain minimal content.
3535const acceptHeader = 'text/html; blazor-enhanced-nav=on' ;
3636
3737let currentEnhancedNavigationAbortController : AbortController | null ;
38- let navigationEnhancementCallbacks : NavigationEnhancementCallbacks ;
38+ let navigationEnhancementCallbacks : Promise < NavigationEnhancementCallbacks > ;
39+ let navigationEnhancementCallbacksResolver : ( callbacks : NavigationEnhancementCallbacks ) => void ;
3940let performingEnhancedPageLoad : boolean ;
4041
42+ // Initialize the promise that will be resolved when callbacks are set
43+ navigationEnhancementCallbacks = new Promise < NavigationEnhancementCallbacks > ( ( resolve ) => {
44+ navigationEnhancementCallbacksResolver = resolve ;
45+ } ) ;
46+
4147// This gets initialized to the current URL when we load.
4248// After that, it gets updated every time we successfully complete a navigation.
4349let currentContentUrl = location . href ;
@@ -57,7 +63,7 @@ export function hasNeverStartedAnyEnhancedPageLoad() {
5763}
5864
5965export function attachProgressivelyEnhancedNavigationListener ( callbacks : NavigationEnhancementCallbacks ) {
60- navigationEnhancementCallbacks = callbacks ;
66+ navigationEnhancementCallbacksResolver ( callbacks ) ;
6167 document . addEventListener ( 'click' , onDocumentClick ) ;
6268 document . addEventListener ( 'submit' , onDocumentSubmit ) ;
6369 window . addEventListener ( 'popstate' , onPopState ) ;
@@ -201,8 +207,11 @@ export async function performEnhancedPageLoad(internalDestinationHref: string, i
201207 // Notify any interactive runtimes that an enhanced navigation is starting
202208 notifyEnhancedNavigationListeners ( internalDestinationHref , interceptedLink ) ;
203209
210+ // Wait for navigation enhancement callbacks to be initialized before proceeding
211+ const callbacks = await navigationEnhancementCallbacks ;
212+
204213 // Notify handlers that enhanced navigation is starting
205- navigationEnhancementCallbacks . enhancedNavigationStarted ( ) ;
214+ callbacks . enhancedNavigationStarted ( ) ;
206215
207216 // Now request the new page via fetch, and a special header that tells the server we want it to inject
208217 // framing boundaries to distinguish the initial document and each subsequent streaming SSR update.
@@ -305,7 +314,7 @@ export async function performEnhancedPageLoad(internalDestinationHref: string, i
305314 // For HTML responses, regardless of the status code, display it
306315 const parsedHtml = new DOMParser ( ) . parseFromString ( initialContent , 'text/html' ) ;
307316 synchronizeDomContent ( document , parsedHtml ) ;
308- navigationEnhancementCallbacks . documentUpdated ( ) ;
317+ callbacks . documentUpdated ( ) ;
309318 } else if ( responseContentType ?. startsWith ( 'text/' ) && initialContent ) {
310319 // For any other text-based content, we'll just display it, because that's what
311320 // would happen if this was a non-enhanced request.
@@ -344,7 +353,7 @@ export async function performEnhancedPageLoad(internalDestinationHref: string, i
344353 }
345354
346355 performingEnhancedPageLoad = false ;
347- navigationEnhancementCallbacks . enhancedNavigationCompleted ( ) ;
356+ callbacks . enhancedNavigationCompleted ( ) ;
348357
349358 // For non-GET requests, the destination has to be the same URL you're already on, or result in a redirection
350359 // (post/redirect/get). You're not allowed to POST to a different URL without redirecting, because then back/forwards
0 commit comments