Skip to content

Commit 10452bf

Browse files
author
N. Taylor Mullen
committed
Improve Blazor reconnection experience.
- Updated text of reconnect dialog to be more clear. - Added user feedback to the `Retry` event button click. The current flow is `Attempting to reconnect` -> `Failed to reconnect ... [Retry]` -> Click Retry -> `Attempting to reconnect`. - Found that in cases where the server went away entirely the reconnect event would through unexpectedly preventing the reconnect display from handling a failed reconnect. Added a separate error flow to understand when the server went away/could not start a SignalR connection to. - Could not find a great way to add tests for this scenario. Addresses #12442
1 parent 13fc89c commit 10452bf

File tree

4 files changed

+19
-8
lines changed

4 files changed

+19
-8
lines changed

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

+2-2
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.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,13 @@ async function boot(userOptions?: Partial<BlazorOptions>): Promise<void> {
3737
}
3838

3939
const reconnection = existingConnection || await initializeConnection(options, logger);
40+
if (reconnection.state !== signalR.HubConnectionState.Connected) {
41+
logger.log(LogLevel.Information, 'Reconnection attempt failed. Unable to connect to the server.');
42+
return false;
43+
}
44+
4045
if (!(await circuit.reconnect(reconnection))) {
41-
logger.log(LogLevel.Information, 'Reconnection attempt failed.');
46+
logger.log(LogLevel.Information, 'Reconnection attempt to the circuit failed.');
4247
return false;
4348
}
4449

src/Components/Web.JS/src/Platform/Circuits/DefaultReconnectDisplay.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,18 @@ export class DefaultReconnectDisplay implements ReconnectDisplay {
3131
];
3232

3333
this.modal.style.cssText = modalStyles.join(';');
34-
this.modal.innerHTML = '<h5 style="margin-top: 20px"></h5><button style="margin:5px auto 5px">Retry?</button><p>Alternatively, <a href>reload</a></p>';
34+
this.modal.innerHTML = '<h5 style="margin-top: 20px"></h5><button style="margin:5px auto 5px">Retry</button><p>Alternatively, <a href>reload</a></p>';
3535
this.message = this.modal.querySelector('h5')!;
3636
this.button = this.modal.querySelector('button')!;
3737
this.reloadParagraph = this.modal.querySelector('p')!;
3838

39-
this.button.addEventListener('click', () => window['Blazor'].reconnect());
39+
this.button.addEventListener('click', async () => {
40+
this.show();
41+
const successful = await window['Blazor'].reconnect();
42+
if (!successful) {
43+
this.failed();
44+
}
45+
});
4046
this.reloadParagraph.querySelector('a')!.addEventListener('click', () => location.reload());
4147
}
4248

@@ -57,7 +63,7 @@ export class DefaultReconnectDisplay implements ReconnectDisplay {
5763

5864
failed(): void {
5965
this.button.style.display = 'block';
60-
this.reloadParagraph.style.display = 'block';
61-
this.message.textContent = 'Failed to reconnect to the server.';
66+
this.reloadParagraph.style.display = 'none';
67+
this.message.innerHTML = 'Reconnection failed. Try <a href>reloading</a> the page if you\'re unable to reconnect.';
6268
}
6369
}

src/Components/Web.JS/tests/DefaultReconnectDisplay.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ describe('DefaultReconnectDisplay', () => {
4545
display.failed();
4646

4747
expect(display.modal.style.display).toBe('block');
48-
expect(display.message.textContent).toBe('Failed to reconnect to the server.');
48+
expect(display.message.innerHTML).toBe('Reconnection failed. Try <a href=\"\">reloading</a> the page if you\'re unable to reconnect.');
4949
expect(display.button.style.display).toBe('block');
5050
});
5151

0 commit comments

Comments
 (0)