Skip to content
This repository was archived by the owner on Jan 6, 2025. It is now read-only.

Commit d4f2514

Browse files
CaerusKaruThomasBurleson
authored andcommitted
fix(ssr): fix lazy-loading functionality
* add global provider to StylesheetMap to prevent lazy loading from instantiating multiple copies * add global provider to MatchMedia to ensure that the right version is instantiated in lazy modules
1 parent bbe4b23 commit d4f2514

File tree

10 files changed

+84
-5
lines changed

10 files changed

+84
-5
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
import {InjectionToken, NgZone, Optional, PLATFORM_ID, SkipSelf} from '@angular/core';
9+
import {DOCUMENT} from '@angular/common';
10+
11+
import {MatchMedia} from '../match-media/match-media';
12+
13+
/**
14+
* Ensure a single global service provider
15+
*/
16+
export function MATCH_MEDIA_PROVIDER_FACTORY(parentMedia: MatchMedia,
17+
ngZone: NgZone,
18+
platformId: Object,
19+
_document: Document) {
20+
return parentMedia || new MatchMedia(ngZone, platformId, _document);
21+
}
22+
23+
24+
/**
25+
* Export provider that uses a global service factory (above)
26+
*/
27+
export const MATCH_MEDIA_PROVIDER = {
28+
provide : MatchMedia,
29+
deps : [
30+
[ new Optional(), new SkipSelf(), MatchMedia ],
31+
NgZone,
32+
<InjectionToken<Object>>PLATFORM_ID,
33+
<InjectionToken<Document>>DOCUMENT,
34+
],
35+
useFactory : MATCH_MEDIA_PROVIDER_FACTORY
36+
};

src/lib/core/module.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
*/
88
import {NgModule} from '@angular/core';
99

10-
import {MatchMedia} from './match-media/match-media';
1110
import {MediaMonitor} from './media-monitor/media-monitor';
1211
import {BreakPointRegistry} from './breakpoints/break-point-registry';
1312

1413
import {OBSERVABLE_MEDIA_PROVIDER} from './observable-media/observable-media-provider';
1514
import {DEFAULT_BREAKPOINTS_PROVIDER} from './breakpoints/break-points-provider';
15+
import {MATCH_MEDIA_PROVIDER} from './match-media/match-media-provider';
1616

1717
/**
1818
* *****************************************************************
@@ -24,7 +24,7 @@ import {DEFAULT_BREAKPOINTS_PROVIDER} from './breakpoints/break-points-provider'
2424
providers: [
2525
DEFAULT_BREAKPOINTS_PROVIDER, // Supports developer overrides of list of known breakpoints
2626
BreakPointRegistry, // Registry of known/used BreakPoint(s)
27-
MatchMedia, // Low-level service to publish observables w/ window.matchMedia()
27+
MATCH_MEDIA_PROVIDER, // Low-level service to publish observables w/ window.matchMedia()
2828
MediaMonitor, // MediaQuery monitor service observes all known breakpoints
2929
OBSERVABLE_MEDIA_PROVIDER // easy subscription injectable `media$` matchMedia observable
3030
]

src/lib/core/public-api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export * from './browser-provider';
1010
export * from './module';
1111
export * from './media-queries-module';
1212
export * from './media-change';
13-
export * from './stylesheet-map';
13+
export * from './stylesheet-map/index';
1414
export * from './server-token';
1515

1616
export * from './base/index';

src/lib/core/style-utils/style-utils.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {ComponentFixture, inject, TestBed} from '@angular/core/testing';
1212
import {customMatchers} from '../../utils/testing/custom-matchers';
1313
import {makeCreateTestComponent, expectNativeEl} from '../../utils/testing/helpers';
1414
import {StyleUtils} from './style-utils';
15-
import {StylesheetMap} from '../stylesheet-map';
15+
import {StylesheetMap} from '../stylesheet-map/stylesheet-map';
1616

1717
describe('styler', () => {
1818
let styler: StyleUtils;

src/lib/core/style-utils/style-utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {Inject, Injectable, Optional, PLATFORM_ID} from '@angular/core';
99
import {isPlatformBrowser, isPlatformServer} from '@angular/common';
1010

1111
import {applyCssPrefixes} from '../../utils/auto-prefixer';
12-
import {StylesheetMap} from '../stylesheet-map';
12+
import {StylesheetMap} from '../stylesheet-map/stylesheet-map';
1313
import {SERVER_TOKEN} from '../server-token';
1414

1515
@Injectable()

src/lib/core/stylesheet-map/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
export * from './stylesheet-map';
10+
export * from './stylesheet-map-provider';
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
import {Optional, SkipSelf} from '@angular/core';
9+
10+
import {StylesheetMap} from './stylesheet-map';
11+
12+
/**
13+
* Ensure a single global service provider
14+
*/
15+
export function STYLESHEET_MAP_PROVIDER_FACTORY(parentSheet: StylesheetMap) {
16+
return parentSheet || new StylesheetMap();
17+
}
18+
19+
20+
/**
21+
* Export provider that uses a global service factory (above)
22+
*/
23+
export const STYLESHEET_MAP_PROVIDER = {
24+
provide: StylesheetMap,
25+
deps: [
26+
[new Optional(), new SkipSelf(), StylesheetMap],
27+
],
28+
useFactory: STYLESHEET_MAP_PROVIDER_FACTORY
29+
};

src/lib/extended/module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {NgModule} from '@angular/core';
99
import {
1010
BROWSER_PROVIDER,
1111
CoreModule,
12+
STYLESHEET_MAP_PROVIDER,
1213
StylesheetMap,
1314
StyleUtils
1415
} from '@angular/flex-layout/core';
@@ -40,6 +41,7 @@ const ALL_DIRECTIVES = [
4041
StylesheetMap,
4142
StyleUtils,
4243
BROWSER_PROVIDER,
44+
STYLESHEET_MAP_PROVIDER,
4345
]
4446
})
4547
export class ExtendedModule {

src/lib/flex/module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {BidiModule} from '@angular/cdk/bidi';
1010
import {
1111
BROWSER_PROVIDER,
1212
CoreModule,
13+
STYLESHEET_MAP_PROVIDER,
1314
StylesheetMap,
1415
StyleUtils
1516
} from '@angular/flex-layout/core';
@@ -49,6 +50,7 @@ const ALL_DIRECTIVES = [
4950
StylesheetMap,
5051
StyleUtils,
5152
BROWSER_PROVIDER,
53+
STYLESHEET_MAP_PROVIDER,
5254
]
5355
})
5456
export class FlexModule {

0 commit comments

Comments
 (0)