Better reconnection logic for Blazor Server · Issue #32113 · dotnet/aspnetcore (original) (raw)
Summary
Currently there is an issue with Blazor Server-side where it is not reconnecting circuits automatically and losing connection especially on mobile browsers.
Motivation and goals
- Automatically reload page on
Reconnection attempt to the circuit was rejected by the server. This may indicate that the associated state is no longer available on the server.
- Automatically reload page on mobile browsers that lost connection (probably due to browser tab being suspended), see Blazor Server Side. JS doesn't start after a long downtime on mobile browsers. #23340 Investigate behavior of Blazor Server on mobile when switching apps #26985
In scope
- Automatically refresh the page when circuit was rejected
- Add
onReconnectionFailed
event onReconnectionFailed
could be used to automatically refresh the page instead of asking user to do that manually like in a screenshot shown below- Change
DefaultReconnectionHandler.attemptPeriodicReconnection
to try to reconnect first and then delay, instead of delaying first and then trying to reconnect. (Possibly could fix Blazor Server Side. JS doesn't start after a long downtime on mobile browsers. #23340 Investigate behavior of Blazor Server on mobile when switching apps #26985)
Out of scope
Risks / unknowns
- Difficult to test on mobile browsers
Examples
DefaultReconnectionHandler.ts
async attemptPeriodicReconnection(options: ReconnectionOptions) { for (let i = 0; i < options.maxRetries; i++) { this.reconnectDisplay.update(i + 1);
const delayDuration = i == 0 && options.retryIntervalMilliseconds > ReconnectionProcess.MaximumFirstRetryInterval
? ReconnectionProcess.MaximumFirstRetryInterval
: options.retryIntervalMilliseconds;
this.logger.log(LogLevel.Debug, `Reconnecting in ${delayDuration}`);
await this.delay(delayDuration);
//code..
//try { reconnect() }
//instead of delaying 3000ms first and trying to reconnect later
//we could try to reconnect first and delay afterwards
//this could possibly fix reconnection on suspended mobile browsers
Example onReconnectionFailed
event default implementation
Blazor.defaultReconnectionHandler.onReconnectionFailed = function (d) {
document.location.reload();
}
I can take care of the implementation of this task.