2017-09-11 03:18:55 -04:00
|
|
|
/**
|
|
|
|
* @license
|
2020-05-19 15:08:49 -04:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2017-09-11 03:18:55 -04:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2019-03-11 20:20:40 -04:00
|
|
|
import {DOCUMENT} from '@angular/common';
|
2017-09-11 03:18:55 -04:00
|
|
|
import {APP_ID, NgModule} from '@angular/core';
|
2019-03-11 20:20:40 -04:00
|
|
|
import {TransferState, ɵescapeHtml as escapeHtml} from '@angular/platform-browser';
|
2017-09-11 03:18:55 -04:00
|
|
|
|
|
|
|
import {BEFORE_APP_SERIALIZED} from './tokens';
|
|
|
|
|
|
|
|
export function serializeTransferStateFactory(
|
|
|
|
doc: Document, appId: string, transferStore: TransferState) {
|
|
|
|
return () => {
|
|
|
|
const script = doc.createElement('script');
|
|
|
|
script.id = appId + '-state';
|
|
|
|
script.setAttribute('type', 'application/json');
|
|
|
|
script.textContent = escapeHtml(transferStore.toJson());
|
|
|
|
doc.body.appendChild(script);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* NgModule to install on the server side while using the `TransferState` to transfer state from
|
|
|
|
* server to client.
|
|
|
|
*
|
2018-10-19 07:12:20 -04:00
|
|
|
* @publicApi
|
2017-09-11 03:18:55 -04:00
|
|
|
*/
|
|
|
|
@NgModule({
|
|
|
|
providers: [
|
|
|
|
TransferState, {
|
|
|
|
provide: BEFORE_APP_SERIALIZED,
|
|
|
|
useFactory: serializeTransferStateFactory,
|
|
|
|
deps: [DOCUMENT, APP_ID, TransferState],
|
|
|
|
multi: true,
|
|
|
|
}
|
|
|
|
]
|
|
|
|
})
|
|
|
|
export class ServerTransferStateModule {
|
|
|
|
}
|