Skip to content

Commit e6cb411

Browse files
crisbetoalxhub
authored andcommitted
fix(platform-browser): automatically disable animations on the server (#59762)
Uses `ngServerMode` to automatically disable browser animations on the server. This allows us to decouple `platform-server` from the animations package. PR Close #59762
1 parent 0c1ecb4 commit e6cb411

2 files changed

Lines changed: 25 additions & 9 deletions

File tree

packages/platform-browser/animations/async/src/providers.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import {
1414
NgZone,
1515
RendererFactory2,
1616
ɵperformanceMarkFeature as performanceMarkFeature,
17-
InjectionToken,
1817
} from '@angular/core';
1918
import {ɵDomRendererFactory2 as DomRendererFactory2} from '@angular/platform-browser';
2019

@@ -51,6 +50,12 @@ export function provideAnimationsAsync(
5150
type: 'animations' | 'noop' = 'animations',
5251
): EnvironmentProviders {
5352
performanceMarkFeature('NgAsyncAnimations');
53+
54+
// Animations don't work on the server so we switch them over to no-op automatically.
55+
if (typeof ngServerMode !== 'undefined' && ngServerMode) {
56+
type = 'noop';
57+
}
58+
5459
return makeEnvironmentProviders([
5560
{
5661
provide: RendererFactory2,

packages/platform-browser/animations/src/providers.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,31 @@ const SHARED_ANIMATION_PROVIDERS: Provider[] = [
7171

7272
/**
7373
* Separate providers from the actual module so that we can do a local modification in Google3 to
74-
* include them in the BrowserModule.
74+
* include them in the BrowserTestingModule.
7575
*/
76-
export const BROWSER_ANIMATIONS_PROVIDERS: Provider[] = [
77-
{provide: AnimationDriver, useFactory: () => new WebAnimationsDriver()},
78-
{provide: ANIMATION_MODULE_TYPE, useValue: 'BrowserAnimations'},
76+
export const BROWSER_NOOP_ANIMATIONS_PROVIDERS: Provider[] = [
77+
{provide: AnimationDriver, useClass: NoopAnimationDriver},
78+
{provide: ANIMATION_MODULE_TYPE, useValue: 'NoopAnimations'},
7979
...SHARED_ANIMATION_PROVIDERS,
8080
];
8181

8282
/**
8383
* Separate providers from the actual module so that we can do a local modification in Google3 to
84-
* include them in the BrowserTestingModule.
84+
* include them in the BrowserModule.
8585
*/
86-
export const BROWSER_NOOP_ANIMATIONS_PROVIDERS: Provider[] = [
87-
{provide: AnimationDriver, useClass: NoopAnimationDriver},
88-
{provide: ANIMATION_MODULE_TYPE, useValue: 'NoopAnimations'},
86+
export const BROWSER_ANIMATIONS_PROVIDERS: Provider[] = [
87+
// Note: the `ngServerMode` happen inside factories to give the variable time to initialize.
88+
{
89+
provide: AnimationDriver,
90+
useFactory: () =>
91+
typeof ngServerMode !== 'undefined' && ngServerMode
92+
? new NoopAnimationDriver()
93+
: new WebAnimationsDriver(),
94+
},
95+
{
96+
provide: ANIMATION_MODULE_TYPE,
97+
useFactory: () =>
98+
typeof ngServerMode !== 'undefined' && ngServerMode ? 'NoopAnimations' : 'BrowserAnimations',
99+
},
89100
...SHARED_ANIMATION_PROVIDERS,
90101
];

0 commit comments

Comments
 (0)