Skip to content

Commit

Permalink
refactor(router): resolve view transition promise in a timeout when u…
Browse files Browse the repository at this point in the history
…nsupported

Related to angular#51131, this change ensures that the router navigation exits
the current event loop before rendering the route when the view transition
feature is enabled, when the browser does not support view transitions.
  • Loading branch information
atscott committed Apr 30, 2024
1 parent fdd560e commit 298de9f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
5 changes: 4 additions & 1 deletion packages/router/src/utils/view_transition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,10 @@ export function createViewTransition(
return injector.get(NgZone).runOutsideAngular(() => {
if (!document.startViewTransition || transitionOptions.skipNextTransition) {
transitionOptions.skipNextTransition = false;
return Promise.resolve();
// The timing of `startViewTransition` is closer to a macrotask. It won't be called
// until the current event loop exits so we use a promise resolved in a timeout instead
// of Promise.resolve() directly.
return new Promise((resolve) => setTimeout(resolve));
}

let resolveViewTransitionStarted: () => void;
Expand Down
3 changes: 3 additions & 0 deletions packages/router/test/view_transitions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ import {Component, destroyPlatform, inject} from '@angular/core';
import {bootstrapApplication} from '@angular/platform-browser';
import {withBody} from '@angular/private/testing';
import {
ActivationStart,
Event,
NavigationEnd,
provideRouter,
Router,
withDisabledInitialNavigation,
withViewTransitions,
} from '@angular/router';
import {BehaviorSubject, Subject, firstValueFrom, skip} from 'rxjs';
import {afterNextNavigation} from '../src/utils/navigations';

describe('view transitions', () => {
if (isNode) {
Expand Down

0 comments on commit 298de9f

Please sign in to comment.