|
| 1 | +import { DOCUMENT } from '@angular/common'; |
| 2 | +import { APP_ID, NgModule } from '@angular/core'; |
| 3 | +import { BEFORE_APP_SERIALIZED } from '@angular/platform-server'; |
| 4 | +import { hydrateDocument } from '@ionic/core/hydrate'; |
| 5 | + |
| 6 | +// @dynamic |
| 7 | +@NgModule({ |
| 8 | + providers: [ |
| 9 | + { |
| 10 | + provide: BEFORE_APP_SERIALIZED, |
| 11 | + useFactory: hydrateIonicComponents, |
| 12 | + multi: true, |
| 13 | + deps: [DOCUMENT, APP_ID] |
| 14 | + } |
| 15 | + ] |
| 16 | +}) |
| 17 | +export class IonicServerModule {} |
| 18 | + |
| 19 | +// @dynamic |
| 20 | +export function hydrateIonicComponents(doc: any, appId: any) { |
| 21 | + return () => { |
| 22 | + return hydrateDocument(doc, { |
| 23 | + clientHydrateAnnotations: false |
| 24 | + }) |
| 25 | + .then(hydrateResults => { |
| 26 | + hydrateResults.diagnostics.forEach(d => { |
| 27 | + if (d.type === 'error') { |
| 28 | + console.error(d.messageText); |
| 29 | + } else if (d.type === 'debug') { |
| 30 | + console.debug(d.messageText); |
| 31 | + } else { |
| 32 | + console.log(d.messageText); |
| 33 | + } |
| 34 | + }); |
| 35 | + |
| 36 | + if (doc.head != null) { |
| 37 | + const styleElms = doc.head.querySelectorAll('style[data-styles]') as NodeListOf<HTMLStyleElement>; |
| 38 | + for (let i = 0; i < styleElms.length; i++) { |
| 39 | + styleElms[i].setAttribute('ng-transition', appId); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + if (doc.body != null) { |
| 44 | + const ionPages = doc.body.querySelectorAll('.ion-page.ion-page-invisible') as NodeListOf<HTMLElement>; |
| 45 | + for (let i = 0; i < ionPages.length; i++) { |
| 46 | + ionPages[i].classList.remove('ion-page-invisible'); |
| 47 | + } |
| 48 | + } |
| 49 | + }); |
| 50 | + }; |
| 51 | +} |
0 commit comments