@@ -29,23 +29,8 @@ async function boot(userOptions?: Partial<CircuitStartOptions>): Promise<void> {
2929 const jsInitializer = await fetchAndInvokeInitializers ( options ) ;
3030
3131 const logger = new ConsoleLogger ( options . logLevel ) ;
32- Blazor . defaultReconnectionHandler = new DefaultReconnectionHandler ( logger ) ;
33-
34- options . reconnectionHandler = options . reconnectionHandler || Blazor . defaultReconnectionHandler ;
35- logger . log ( LogLevel . Information , 'Starting up Blazor server-side application.' ) ;
36-
37- const components = discoverComponents ( document , 'server' ) as ServerComponentDescriptor [ ] ;
38- const appState = discoverPersistedState ( document ) ;
39- const circuit = new CircuitDescriptor ( components , appState || '' ) ;
4032
41- const initialConnection = await initializeConnection ( options , logger , circuit ) ;
42- const circuitStarted = await circuit . startCircuit ( initialConnection ) ;
43- if ( ! circuitStarted ) {
44- logger . log ( LogLevel . Error , 'Failed to start the circuit.' ) ;
45- return ;
46- }
47-
48- const reconnect = async ( existingConnection ?: HubConnection ) : Promise < boolean > => {
33+ Blazor . reconnect = async ( existingConnection ?: HubConnection ) : Promise < boolean > => {
4934 if ( renderingFailed ) {
5035 // We can't reconnect after a failure, so exit early.
5136 return false ;
@@ -61,6 +46,21 @@ async function boot(userOptions?: Partial<CircuitStartOptions>): Promise<void> {
6146
6247 return true ;
6348 } ;
49+ Blazor . defaultReconnectionHandler = new DefaultReconnectionHandler ( logger ) ;
50+
51+ options . reconnectionHandler = options . reconnectionHandler || Blazor . defaultReconnectionHandler ;
52+ logger . log ( LogLevel . Information , 'Starting up Blazor server-side application.' ) ;
53+
54+ const components = discoverComponents ( document , 'server' ) as ServerComponentDescriptor [ ] ;
55+ const appState = discoverPersistedState ( document ) ;
56+ const circuit = new CircuitDescriptor ( components , appState || '' ) ;
57+
58+ const initialConnection = await initializeConnection ( options , logger , circuit ) ;
59+ const circuitStarted = await circuit . startCircuit ( initialConnection ) ;
60+ if ( ! circuitStarted ) {
61+ logger . log ( LogLevel . Error , 'Failed to start the circuit.' ) ;
62+ return ;
63+ }
6464
6565 let disconnectSent = false ;
6666 const cleanup = ( ) => {
@@ -76,8 +76,6 @@ async function boot(userOptions?: Partial<CircuitStartOptions>): Promise<void> {
7676
7777 window . addEventListener ( 'unload' , cleanup , { capture : false , once : true } ) ;
7878
79- Blazor . reconnect = reconnect ;
80-
8179 logger . log ( LogLevel . Information , 'Blazor server-side application started.' ) ;
8280
8381 jsInitializer . invokeAfterStartedCallbacks ( Blazor ) ;
@@ -138,10 +136,15 @@ async function initializeConnection(options: CircuitStartOptions, logger: Logger
138136
139137 try {
140138 await connection . start ( ) ;
141- } catch ( ex ) {
139+ } catch ( ex : any ) {
142140 unhandledError ( connection , ex as Error , logger ) ;
143141
144- if ( ! isNestedError ( ex ) ) {
142+ if ( ex . errorType === 'FailedToNegotiateWithServerError' ) {
143+ // Connection with the server has been interrupted, and we're in the process of reconnecting.
144+ // Throw this exception so it can be handled at the reconnection layer, and don't show the
145+ // error notification.
146+ throw ex ;
147+ } else if ( ! isNestedError ( ex ) ) {
145148 showErrorNotification ( ) ;
146149 } else if ( ex . innerErrors && ex . innerErrors . some ( e => e . errorType === 'UnsupportedTransportError' && e . transport === HttpTransportType . WebSockets ) ) {
147150 showErrorNotification ( 'Unable to connect, please ensure you are using an updated browser that supports WebSockets.' ) ;
0 commit comments