From 5bef240bffc0d63d7dbd632c27782e36182c76c5 Mon Sep 17 00:00:00 2001 From: milanmajchrak Date: Thu, 26 Nov 2026 15:50:09 +0100 Subject: [PATCH 1/9] Enable Angular hydration for server-rendered pages Provide hydration on the server too, so the browser reuses the server DOM. ThemedComponent lets Angular remove its wrapper and skips hydration when content is projected. Co-Authored-By: Claude Opus 5.5 --- src/app/app.config.ts | 7 +++ .../test/custom/themed-test.component.spec.ts | 2 +- .../theme-support/themed.component.html | 8 ++- .../theme-support/themed.component.spec.ts | 63 +++++++++++++++++++ .../shared/theme-support/themed.component.ts | 12 +++- src/modules/app/browser-app.config.ts | 2 - 6 files changed, 86 insertions(+), 8 deletions(-) diff --git a/src/app/app.config.ts b/src/app/app.config.ts index 33a5f830c10..ebe0dae3d1b 100644 --- a/src/app/app.config.ts +++ b/src/app/app.config.ts @@ -5,6 +5,10 @@ import { DOCUMENT, importProvidersFrom, } from '@angular/core'; +import { + provideClientHydration, + withNoHttpTransferCache, +} from '@angular/platform-browser'; import { NoPreloading, provideRouter, @@ -115,6 +119,9 @@ export const commonAppConfig: ApplicationConfig = { withPreloading(NoPreloading), withComponentInputBinding(), ), + // Needed on the server too, or the browser gets no hydration data and draws the page again. + // REST responses already reach the browser through the NgRx transfer state. + provideClientHydration(withNoHttpTransferCache()), { provide: APP_BASE_HREF, useFactory: getBaseHref, diff --git a/src/app/shared/theme-support/test/custom/themed-test.component.spec.ts b/src/app/shared/theme-support/test/custom/themed-test.component.spec.ts index 7b9abefadb8..e08c7aa4924 100644 --- a/src/app/shared/theme-support/test/custom/themed-test.component.spec.ts +++ b/src/app/shared/theme-support/test/custom/themed-test.component.spec.ts @@ -3,7 +3,7 @@ import { Component } from '@angular/core'; // noinspection AngularMissingOrInvalidDeclarationInModule @Component({ selector: 'ds-test-component', - template: '', + template: '', }) export class TestComponent { type = 'themed'; diff --git a/src/app/shared/theme-support/themed.component.html b/src/app/shared/theme-support/themed.component.html index a39b288f0e4..a6b40709126 100644 --- a/src/app/shared/theme-support/themed.component.html +++ b/src/app/shared/theme-support/themed.component.html @@ -1,5 +1,7 @@ -
- -
+@if (!compRef) { +
+ +
+} diff --git a/src/app/shared/theme-support/themed.component.spec.ts b/src/app/shared/theme-support/themed.component.spec.ts index d761ad95b0e..b1ff4bb6d53 100644 --- a/src/app/shared/theme-support/themed.component.spec.ts +++ b/src/app/shared/theme-support/themed.component.spec.ts @@ -1,6 +1,7 @@ /* eslint-disable max-classes-per-file */ import { Component, + DebugElement, NO_ERRORS_SCHEMA, } from '@angular/core'; import { @@ -8,8 +9,12 @@ import { TestBed, waitForAsync, } from '@angular/core/testing'; +import { By } from '@angular/platform-browser'; +import { firstValueFrom } from 'rxjs'; +import { filter } from 'rxjs/operators'; import { ThemeConfig } from '../../../config/theme.config'; +import { hasValue } from '../empty.util'; import { getMockThemeService } from '../mocks/theme-service.mock'; import { VarDirective } from '../utils/var.directive'; import { TestComponent } from './test/test.component.spec'; @@ -36,6 +41,16 @@ class TestThemedComponent extends ThemedComponent { } } +@Component({ + selector: 'ds-test-themed-host', + template: 'projected', + imports: [ + TestThemedComponent, + ], +}) +class TestThemedHostComponent { +} + describe('ThemedComponent', () => { let component: TestThemedComponent; let fixture: ComponentFixture; @@ -60,6 +75,54 @@ describe('ThemedComponent', () => { fixture.detectChanges(); } + describe('hydration', () => { + beforeEach(waitForAsync(() => { + setupTestingModuleForTheme('custom'); + })); + + async function whenRendered(themed: TestThemedComponent, themedFixture: ComponentFixture) { + await firstValueFrom(themed.compRef$.pipe(filter((compRef) => hasValue(compRef)))); + themedFixture.detectChanges(); + } + + async function renderHost(): Promise { + const hostFixture = TestBed.createComponent(TestThemedHostComponent); + hostFixture.detectChanges(); + const themed = hostFixture.debugElement.query(By.directive(TestThemedComponent)); + await whenRendered(themed.componentInstance, hostFixture); + return themed; + } + + it('should not skip hydration when no content is projected', async () => { + initComponent(); + await whenRendered(component, fixture); + expect(fixture.nativeElement.hasAttribute('ngSkipHydration')).toBeFalse(); + }); + + it('should skip hydration when content is projected', async () => { + const themed = await renderHost(); + expect(themed.nativeElement.hasAttribute('ngSkipHydration')).toBeTrue(); + }); + + it('should project the content into the themed component', async () => { + const themed = await renderHost(); + expect(themed.nativeElement.querySelector(':scope > ds-test-component > span')?.textContent).toBe('projected'); + }); + + it('should remove the content wrapper once the themed component is rendered', async () => { + initComponent(); + await whenRendered(component, fixture); + expect(fixture.nativeElement.querySelector(':scope > div')).toBeNull(); + }); + + it('should not keep elements that are no longer in the DOM', async () => { + initComponent(); + await whenRendered(component, fixture); + const detached = fixture.debugElement.queryAll(By.all()).filter((el) => !el.nativeElement.isConnected); + expect(detached.map((el) => el.name)).toEqual([]); + }); + }); + describe('when the current theme matches a themed component', () => { beforeEach(waitForAsync(() => { setupTestingModuleForTheme('custom'); diff --git a/src/app/shared/theme-support/themed.component.ts b/src/app/shared/theme-support/themed.component.ts index e56277db227..5d1ae7fc8e7 100644 --- a/src/app/shared/theme-support/themed.component.ts +++ b/src/app/shared/theme-support/themed.component.ts @@ -62,6 +62,12 @@ export abstract class ThemedComponent implements AfterViewInit */ @HostBinding('attr.data-used-theme') usedTheme: string; + /** + * Set when content is projected: Angular can't hydrate DOM nodes passed to createComponent, + * so this wrapper is rendered again in the browser instead. + */ + @HostBinding('attr.ngSkipHydration') skipHydration: boolean; + constructor( protected cdr: ChangeDetectorRef, protected themeService: ThemeService, @@ -136,8 +142,11 @@ export abstract class ThemedComponent implements AfterViewInit this.lazyLoadSub = this.lazyLoadObs.subscribe(([simpleChanges, constructor]: [SimpleChanges, GenericConstructor]) => { this.destroyComponentInstance(); + // the content wrapper is gone once a component was rendered, see themed.component.html + const projectedNodes = this.themedElementContent?.nativeElement.childNodes ?? []; + this.skipHydration = projectedNodes.length > 0 ? true : null; this.compRef = this.vcr.createComponent(constructor, { - projectableNodes: [this.themedElementContent.nativeElement.childNodes], + projectableNodes: [projectedNodes], }); if (hasValue(simpleChanges)) { this.ngOnChanges(simpleChanges); @@ -146,7 +155,6 @@ export abstract class ThemedComponent implements AfterViewInit } this.compRef$.next(this.compRef); this.cdr.markForCheck(); - this.themedElementContent.nativeElement.remove(); }); } diff --git a/src/modules/app/browser-app.config.ts b/src/modules/app/browser-app.config.ts index 8743a596372..98ad2dbed3b 100644 --- a/src/modules/app/browser-app.config.ts +++ b/src/modules/app/browser-app.config.ts @@ -15,7 +15,6 @@ import { provideAppInitializer, TransferState, } from '@angular/core'; -import { provideClientHydration } from '@angular/platform-browser'; import { provideAnimations } from '@angular/platform-browser/animations'; import { EffectsModule } from '@ngrx/effects'; import { @@ -91,7 +90,6 @@ export const browserAppConfig: ApplicationConfig = mergeApplicationConfig({ providers: [ provideHttpClient(withInterceptorsFromDi()), provideAnimations(), - provideClientHydration(), importProvidersFrom( // forRoot ensures the providers are only created once Angulartics2RouterlessModule.forRoot(), From 6699620caa0f6c16ef0ef9a8df7ba92a6ad838a1 Mon Sep 17 00:00:00 2001 From: milanmajchrak Date: Thu, 26 Nov 2026 17:19:37 +0100 Subject: [PATCH 2/9] Keep space for home page boxes that load in the browser The facet lists and the new items box keep their loaded height while empty, so the footer and the content below do not jump when they fill. Co-Authored-By: Claude Opus 5.5 --- src/app/home-page/home-page.component.html | 8 +-- src/app/home-page/home-page.component.scss | 17 +++++ src/app/home-page/home-page.component.spec.ts | 72 +++++++++++++++++++ 3 files changed, 93 insertions(+), 4 deletions(-) create mode 100644 src/app/home-page/home-page.component.spec.ts diff --git a/src/app/home-page/home-page.component.html b/src/app/home-page/home-page.component.html index 005844a5936..ed053554fc0 100644 --- a/src/app/home-page/home-page.component.html +++ b/src/app/home-page/home-page.component.html @@ -76,7 +76,7 @@