From 9a0630e360873b0ea86a7341572b83672f4bb652 Mon Sep 17 00:00:00 2001 From: cipchk Date: Sun, 12 Nov 2023 13:05:15 +0800 Subject: [PATCH] chore(theme:preloader): support ssr --- .../src/services/preloader/preloader.spec.ts | 35 ++++++++++++++++++- .../theme/src/services/preloader/preloader.ts | 11 +++--- 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/packages/theme/src/services/preloader/preloader.spec.ts b/packages/theme/src/services/preloader/preloader.spec.ts index 4e32ad313..dd264c0d2 100644 --- a/packages/theme/src/services/preloader/preloader.spec.ts +++ b/packages/theme/src/services/preloader/preloader.spec.ts @@ -1,5 +1,5 @@ import { DOCUMENT } from '@angular/common'; -import { EnvironmentInjector, Injector, runInInjectionContext } from '@angular/core'; +import { EnvironmentInjector, Injector, PLATFORM_ID, runInInjectionContext } from '@angular/core'; import { NzSafeAny } from 'ng-zorro-antd/core/types'; @@ -30,6 +30,7 @@ describe('theme: preloader', () => { const preloader = document.querySelector('.preloader')!; const injector = Injector.create({ providers: [ + { provide: PLATFORM_ID, useValue: 'browser' }, { provide: DOCUMENT, useFactory: () => { @@ -64,6 +65,7 @@ describe('theme: preloader', () => { const injector = Injector.create({ providers: [ + { provide: PLATFORM_ID, useValue: 'browser' }, { provide: DOCUMENT, useFactory: () => { @@ -77,4 +79,35 @@ describe('theme: preloader', () => { preloaderDone(); expect(document.querySelector('.preloader')).toBeNull(); }); + + it('#ssr', () => { + spyOn(document, 'querySelector').and.callFake((type: string) => { + if (type === '.preloader') return null; + if (cached[type]) return cached[type]; + cached[type] = { + className: [], + style: { + overflow: '' + }, + addEventListener: (_key: string, fn: NzSafeAny) => { + fn(); + } + }; + return cached[type]; + }); + + const injector = Injector.create({ + providers: [ + { provide: PLATFORM_ID, useValue: 'server' }, + { + provide: DOCUMENT, + useFactory: () => { + return document; + } + } + ] + }) as EnvironmentInjector; + runInInjectionContext(injector, () => stepPreloader()); + expect(document.querySelector('body')?.style.overflow).toBe(''); + }); }); diff --git a/packages/theme/src/services/preloader/preloader.ts b/packages/theme/src/services/preloader/preloader.ts index ac757c1c8..624afa1d4 100644 --- a/packages/theme/src/services/preloader/preloader.ts +++ b/packages/theme/src/services/preloader/preloader.ts @@ -1,9 +1,13 @@ -import { DOCUMENT } from '@angular/common'; -import { inject } from '@angular/core'; +import { DOCUMENT, isPlatformServer } from '@angular/common'; +import { PLATFORM_ID, inject } from '@angular/core'; export function stepPreloader(): () => void { const doc: Document = inject(DOCUMENT); - const body = doc.querySelector('body')!; + const ssr = isPlatformServer(inject(PLATFORM_ID)); + if (ssr) { + return () => {}; + } + const body = doc.querySelector('body')!; body.style.overflow = 'hidden'; let done = false; @@ -19,7 +23,6 @@ export function stepPreloader(): () => void { preloader.className = CLS; }); preloader.className += ` ${CLS}-add ${CLS}-add-active`; - const body = doc.querySelector('body')!; body.style.overflow = ''; }; }