Skip to content

Commit 66a5195

Browse files
committed
awaitStability feature for route redirects
1 parent 966922b commit 66a5195

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

src/Umbraco.Web.UI.Client/src/apps/backoffice/components/backoffice-main.element.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ export class UmbBackofficeMainElement extends UmbLitElement {
6565
newRoutes.push({
6666
path: '',
6767
pathMatch: 'full',
68+
awaitStability: true,
6869
redirectTo: newRoutes[0].path,
6970
});
7071

src/Umbraco.Web.UI.Client/src/packages/core/router/router-slot/model.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ export interface IRedirectRoute<D = any> extends IRouteBase<D> {
7575
// The paths the route should redirect to. Can either be relative or absolute.
7676
redirectTo: string;
7777

78+
// First redirect when the routes appears stable. Delaying the redirect so other routes get the change to resolve first.
79+
awaitStability?: boolean;
80+
7881
// Whether the query should be preserved when redirecting.
7982
preserveQuery?: boolean;
8083
}

src/Umbraco.Web.UI.Client/src/packages/core/router/router-slot/router-slot.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,24 @@ export class RouterSlot<D = any, P = any> extends HTMLElement implements IRouter
314314
}
315315
}
316316

317+
private getRedirectDelay() {
318+
if ('connection' in navigator) {
319+
const connection =
320+
navigator.connection || (navigator as any).mozConnection || (navigator as any).webkitConnection;
321+
322+
switch (connection.effectiveType) {
323+
case 'slow-2g':
324+
case '2g':
325+
return 1200;
326+
case '3g':
327+
return 800;
328+
case '4g':
329+
return 200;
330+
}
331+
}
332+
return 400;
333+
}
334+
317335
/**
318336
* Loads a new path based on the routes.
319337
* Returns true if a navigation was made to a new page.
@@ -387,6 +405,14 @@ export class RouterSlot<D = any, P = any> extends HTMLElement implements IRouter
387405
// Redirect if necessary
388406
if (isRedirectRoute(route)) {
389407
cleanup();
408+
if (route.awaitStability === true) {
409+
// await until browser is done loading, based on a guess:
410+
const delay = this.getRedirectDelay();
411+
await new Promise((resolve) => setTimeout(resolve, delay));
412+
if (navigationInvalidated) {
413+
return cancel();
414+
}
415+
}
390416
handleRedirect(this, route);
391417
return false;
392418
}

0 commit comments

Comments
 (0)