|
6 | 6 | * found in the LICENSE file at https://angular.io/license |
7 | 7 | */ |
8 | 8 |
|
9 | | -import {APP_ID} from './application_tokens'; |
| 9 | +import {APP_ID, PLATFORM_ID} from './application_tokens'; |
10 | 10 | import {inject} from './di/injector_compatibility'; |
11 | 11 | import {ɵɵdefineInjectable} from './di/interface/defs'; |
12 | 12 | import {getDocument} from './render3/interfaces/document'; |
@@ -72,7 +72,10 @@ export function makeStateKey<T = void>(key: string): StateKey<T> { |
72 | 72 |
|
73 | 73 | function initTransferState() { |
74 | 74 | 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 | + |
76 | 79 | return transferState; |
77 | 80 | } |
78 | 81 |
|
@@ -101,7 +104,7 @@ export class TransferState { |
101 | 104 | }); |
102 | 105 |
|
103 | 106 | /** @internal */ |
104 | | - store: {[k: string]: unknown|undefined} = {}; |
| 107 | + store: Record<string, unknown|undefined> = {}; |
105 | 108 |
|
106 | 109 | private onSerializeCallbacks: {[k: string]: () => unknown | undefined} = {}; |
107 | 110 |
|
@@ -165,18 +168,18 @@ export class TransferState { |
165 | 168 | } |
166 | 169 | } |
167 | 170 |
|
168 | | -function retrieveTransferredState(doc: Document, appId: string) { |
| 171 | +function retrieveTransferredState(doc: Document, appId: string): Record<string, unknown|undefined> { |
169 | 172 | // Locate the script tag with the JSON data transferred from the server. |
170 | 173 | // The id of the script tag is set to the Angular appId + 'state'. |
171 | 174 | const script = doc.getElementById(appId + '-state'); |
172 | | - let initialState = {}; |
173 | | - if (script && script.textContent) { |
| 175 | + if (script?.textContent) { |
174 | 176 | try { |
175 | 177 | // 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 {}; |
177 | 179 | } catch (e) { |
178 | 180 | console.warn('Exception while restoring TransferState for app ' + appId, e); |
179 | 181 | } |
180 | 182 | } |
181 | | - return initialState; |
| 183 | + |
| 184 | + return {}; |
182 | 185 | } |
0 commit comments