Skip to content
This repository was archived by the owner on Nov 22, 2024. It is now read-only.

Commit 5aef476

Browse files
author
Fabian Wiles
authored
feat(common): introduce StateTransferInitializerModule (#916)
1 parent 23a40fa commit 5aef476

3 files changed

Lines changed: 42 additions & 0 deletions

File tree

modules/common/public-api.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88
export { TransferHttpCacheModule } from './src/transfer_http';
9+
export { StateTransferInitializerModule } from './src/state-transfer-initilizer/module';
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# State Transfer Initializer
2+
Delays the app bootstrap process to ensure that the DOM content is loaded before state transfer initilization
3+
4+
Simply import the module into your project and you will no longer need to wrap your component bootstrap function in an `DOMContentLoaded` callback
5+
6+
```
7+
import { StateTransferInitializerModule } from '@nguniversal/common'
8+
9+
@NgModule({
10+
imports: [ StateTransferInitializerModule ]
11+
})
12+
export class AppModule{}
13+
```
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import { NgModule, APP_INITIALIZER } from '@angular/core';
10+
import { DOCUMENT } from '@angular/common';
11+
12+
export function domContentLoadedFactory(doc: Document) {
13+
return () => new Promise ((resolve, _reject) => {
14+
const contentLoaded = () => {
15+
doc.removeEventListener('DOMContentLoaded', contentLoaded);
16+
resolve();
17+
};
18+
doc.addEventListener('DOMContentLoaded', contentLoaded);
19+
});
20+
}
21+
22+
23+
@NgModule({
24+
providers: [
25+
{provide: APP_INITIALIZER, multi: true, useFactory: domContentLoadedFactory, deps: [DOCUMENT]},
26+
]
27+
})
28+
export class StateTransferInitializerModule {}

0 commit comments

Comments
 (0)