Skip to content

Commit c934a8e

Browse files
alan-agius4pkozlowski-opensource
authored andcommitted
fix(platform-browser): only add ng-app-id to style on server side (#49465)
This commit fixes an issue where it causes issues in G3 when we add `ng-app-id` on browser apps. PR Close #49465
1 parent 20f7187 commit c934a8e

File tree

4 files changed

+21
-12
lines changed

4 files changed

+21
-12
lines changed

goldens/size-tracking/integration-payloads.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"cli-hello-world": {
33
"uncompressed": {
44
"runtime": 908,
5-
"main": 128561,
5+
"main": 129184,
66
"polyfills": 33792
77
}
88
},
@@ -41,14 +41,14 @@
4141
"animations": {
4242
"uncompressed": {
4343
"runtime": 898,
44-
"main": 159415,
44+
"main": 159979,
4545
"polyfills": 33782
4646
}
4747
},
4848
"standalone-bootstrap": {
4949
"uncompressed": {
5050
"runtime": 918,
51-
"main": 86975,
51+
"main": 87601,
5252
"polyfills": 33802
5353
}
5454
},

integration/platform-server/e2e/helloworld-spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe('Hello world E2E Tests', function() {
2828
expect(element(by.css('div')).getText()).toEqual('Hello world!');
2929

3030
// Make sure the server styles get reused by the client.
31-
expect(element(by.css('style[ng-app-id="ng"]')).isPresent()).toBeTruthy();
31+
expect(element(by.css('style[ng-app-id="ng"]')).isPresent()).toBeFalsy();
3232
expect(element(by.css('style[ng-style-reused]')).isPresent()).toBeTruthy();
3333
expect(element(by.css('style')).getText()).toBe('');
3434

packages/platform-browser/src/dom/shared_styles_host.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {DOCUMENT} from '@angular/common';
10-
import {APP_ID, CSP_NONCE, Inject, Injectable, OnDestroy, Optional} from '@angular/core';
9+
import {DOCUMENT, isPlatformServer} from '@angular/common';
10+
import {APP_ID, CSP_NONCE, Inject, Injectable, OnDestroy, Optional, PLATFORM_ID} from '@angular/core';
1111

1212
/** The style elements attribute name used to set value of `APP_ID` token. */
1313
const APP_ID_ATTRIBUTE_NAME = 'ng-app-id';
@@ -22,12 +22,15 @@ export class SharedStylesHost implements OnDestroy {
2222
> ();
2323
private readonly hostNodes = new Set<Node>();
2424
private readonly styleNodesInDOM: Map<string, HTMLStyleElement>|null;
25+
private readonly platformIsServer: boolean;
2526

2627
constructor(
2728
@Inject(DOCUMENT) private readonly doc: Document,
2829
@Inject(APP_ID) private readonly appId: string,
29-
@Inject(CSP_NONCE) @Optional() private nonce?: string|null) {
30+
@Inject(CSP_NONCE) @Optional() private nonce?: string|null,
31+
@Inject(PLATFORM_ID) readonly platformId: object = {}) {
3032
this.styleNodesInDOM = this.collectServerRenderedStyles();
33+
this.platformIsServer = isPlatformServer(platformId);
3134
this.resetHostNodes();
3235
}
3336

@@ -132,6 +135,8 @@ export class SharedStylesHost implements OnDestroy {
132135
// `styleNodesInDOM` cannot be undefined due to the above `styleNodesInDOM?.get`.
133136
styleNodesInDOM!.delete(style);
134137

138+
styleEl.removeAttribute(APP_ID_ATTRIBUTE_NAME);
139+
135140
if (typeof ngDevMode === 'undefined' || ngDevMode) {
136141
// This attribute is solely used for debugging purposes.
137142
styleEl.setAttribute('ng-style-reused', '');
@@ -147,7 +152,11 @@ export class SharedStylesHost implements OnDestroy {
147152
}
148153

149154
styleEl.textContent = style;
150-
styleEl.setAttribute(APP_ID_ATTRIBUTE_NAME, this.appId);
155+
156+
if (this.platformIsServer) {
157+
styleEl.setAttribute(APP_ID_ATTRIBUTE_NAME, this.appId);
158+
}
159+
151160
return styleEl;
152161
}
153162
}

packages/platform-browser/test/dom/shared_styles_host_spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,20 @@ import {expect} from '@angular/platform-browser/testing/src/matchers';
2525
it('should add existing styles to new hosts', () => {
2626
ssh.addStyles(['a {};']);
2727
ssh.addHost(someHost);
28-
expect(someHost.innerHTML).toEqual('<style ng-app-id="app-id">a {};</style>');
28+
expect(someHost.innerHTML).toEqual('<style>a {};</style>');
2929
});
3030

3131
it('should add new styles to hosts', () => {
3232
ssh.addHost(someHost);
3333
ssh.addStyles(['a {};']);
34-
expect(someHost.innerHTML).toEqual('<style ng-app-id="app-id">a {};</style>');
34+
expect(someHost.innerHTML).toEqual('<style>a {};</style>');
3535
});
3636

3737
it('should add styles only once to hosts', () => {
3838
ssh.addStyles(['a {};']);
3939
ssh.addHost(someHost);
4040
ssh.addStyles(['a {};']);
41-
expect(someHost.innerHTML).toEqual('<style ng-app-id="app-id">a {};</style>');
41+
expect(someHost.innerHTML).toEqual('<style>a {};</style>');
4242
});
4343

4444
it('should use the document head as default host', () => {
@@ -49,7 +49,7 @@ import {expect} from '@angular/platform-browser/testing/src/matchers';
4949
it('should remove style nodes on destroy', () => {
5050
ssh.addStyles(['a {};']);
5151
ssh.addHost(someHost);
52-
expect(someHost.innerHTML).toEqual('<style ng-app-id="app-id">a {};</style>');
52+
expect(someHost.innerHTML).toEqual('<style>a {};</style>');
5353

5454
ssh.ngOnDestroy();
5555
expect(someHost.innerHTML).toEqual('');

0 commit comments

Comments
 (0)