Skip to content

Commit

Permalink
chore(theme:preloader): support ssr
Browse files Browse the repository at this point in the history
  • Loading branch information
cipchk committed Nov 12, 2023
1 parent 1dce440 commit 9a0630e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
35 changes: 34 additions & 1 deletion packages/theme/src/services/preloader/preloader.spec.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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: () => {
Expand Down Expand Up @@ -64,6 +65,7 @@ describe('theme: preloader', () => {

const injector = Injector.create({
providers: [
{ provide: PLATFORM_ID, useValue: 'browser' },
{
provide: DOCUMENT,
useFactory: () => {
Expand All @@ -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<HTMLBodyElement>('body')?.style.overflow).toBe('');
});
});
11 changes: 7 additions & 4 deletions packages/theme/src/services/preloader/preloader.ts
Original file line number Diff line number Diff line change
@@ -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<HTMLBodyElement>('body')!;
body.style.overflow = 'hidden';
let done = false;

Expand All @@ -19,7 +23,6 @@ export function stepPreloader(): () => void {
preloader.className = CLS;
});
preloader.className += ` ${CLS}-add ${CLS}-add-active`;
const body = doc.querySelector<HTMLBodyElement>('body')!;
body.style.overflow = '';
};
}

0 comments on commit 9a0630e

Please sign in to comment.