-
Notifications
You must be signed in to change notification settings - Fork 24.8k
/
transfer_state.ts
55 lines (48 loc) 路 1.8 KB
/
transfer_state.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {DOCUMENT} from '@angular/common';
import {APP_ID, NgModule, Provider} from '@angular/core';
import {TransferState, 傻escapeHtml as escapeHtml} from '@angular/platform-browser';
import {BEFORE_APP_SERIALIZED} from './tokens';
export const TRANSFER_STATE_SERIALIZATION_PROVIDERS: Provider[] = [{
provide: BEFORE_APP_SERIALIZED,
useFactory: serializeTransferStateFactory,
deps: [DOCUMENT, APP_ID, TransferState],
multi: true,
}];
function serializeTransferStateFactory(doc: Document, appId: string, transferStore: TransferState) {
return () => {
// The `.toJSON` here causes the `onSerialize` callbacks to be called.
// These callbacks can be used to provide the value for a given key.
const content = transferStore.toJson();
if (transferStore.isEmpty) {
// The state is empty, nothing to transfer,
// avoid creating an extra `<script>` tag in this case.
return;
}
const script = doc.createElement('script');
script.id = appId + '-state';
script.setAttribute('type', 'application/json');
script.textContent = escapeHtml(content);
doc.body.appendChild(script);
};
}
/**
* NgModule to install on the server side while using the `TransferState` to transfer state from
* server to client.
*
* Note: this module is not needed if the `renderApplication` function is used.
* The `renderApplication` makes all providers from this module available in the application.
*
* @publicApi
* @deprecated no longer needed, you can inject the `TransferState` in an app without providing
* this module.
*/
@NgModule({})
export class ServerTransferStateModule {
}