Skip to content

Commit 346ab73

Browse files
alan-agius4alxhub
authored andcommitted
fix(core): only try to retrieve transferred state on the browser (#50144)
Prior to this commit we tried to retrieve transferred state on both browser and server. Doing this on the server was redundant and could causes issues as `document` might be undefined. Closes #50138 PR Close #50144
1 parent 1686c25 commit 346ab73

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

packages/core/src/transfer_state.ts

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

9-
import {APP_ID} from './application_tokens';
9+
import {APP_ID, PLATFORM_ID} from './application_tokens';
1010
import {inject} from './di/injector_compatibility';
1111
import {ɵɵdefineInjectable} from './di/interface/defs';
1212
import {getDocument} from './render3/interfaces/document';
@@ -72,7 +72,10 @@ export function makeStateKey<T = void>(key: string): StateKey<T> {
7272

7373
function initTransferState() {
7474
const transferState = new TransferState();
75-
transferState.store = retrieveTransferredState(getDocument(), inject(APP_ID));
75+
if (inject(PLATFORM_ID) === 'browser') {
76+
transferState.store = retrieveTransferredState(getDocument(), inject(APP_ID));
77+
}
78+
7679
return transferState;
7780
}
7881

@@ -101,7 +104,7 @@ export class TransferState {
101104
});
102105

103106
/** @internal */
104-
store: {[k: string]: unknown|undefined} = {};
107+
store: Record<string, unknown|undefined> = {};
105108

106109
private onSerializeCallbacks: {[k: string]: () => unknown | undefined} = {};
107110

@@ -165,18 +168,18 @@ export class TransferState {
165168
}
166169
}
167170

168-
function retrieveTransferredState(doc: Document, appId: string) {
171+
function retrieveTransferredState(doc: Document, appId: string): Record<string, unknown|undefined> {
169172
// Locate the script tag with the JSON data transferred from the server.
170173
// The id of the script tag is set to the Angular appId + 'state'.
171174
const script = doc.getElementById(appId + '-state');
172-
let initialState = {};
173-
if (script && script.textContent) {
175+
if (script?.textContent) {
174176
try {
175177
// Avoid using any here as it triggers lint errors in google3 (any is not allowed).
176-
initialState = JSON.parse(unescapeTransferStateContent(script.textContent)) as {};
178+
return JSON.parse(unescapeTransferStateContent(script.textContent)) as {};
177179
} catch (e) {
178180
console.warn('Exception while restoring TransferState for app ' + appId, e);
179181
}
180182
}
181-
return initialState;
183+
184+
return {};
182185
}

packages/core/test/transfer_state_spec.ts

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

9-
import {APP_ID as APP_ID_TOKEN} from '@angular/core';
9+
import {APP_ID as APP_ID_TOKEN, PLATFORM_ID} from '@angular/core';
1010
import {TestBed} from '@angular/core/testing';
1111

1212
import {getDocument} from '../src/render3/interfaces/document';
@@ -44,7 +44,8 @@ describe('TransferState', () => {
4444
beforeEach(() => {
4545
doc = getDocument();
4646
TestBed.configureTestingModule({
47-
providers: [{provide: APP_ID_TOKEN, useValue: APP_ID}],
47+
providers:
48+
[{provide: APP_ID_TOKEN, useValue: APP_ID}, {provide: PLATFORM_ID, useValue: 'browser'}],
4849
});
4950
});
5051

0 commit comments

Comments
 (0)