Skip to content

[dtq-dev-93] Enable Angular hydration so pages are not drawn again after load - #1765

Open
milanmajchrak wants to merge 9 commits into
dtq-dev-93from
93/fe-ssr-hydration
Open

milanmajchrak wants to merge 9 commits into
dtq-dev-93from
93/fe-ssr-hydration

Conversation

@milanmajchrak

@milanmajchrak milanmajchrak commented Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator

References

Replaces the 7.6 anti-flicker overlay from #1287 (5c9c6bc), which was not ported to v9. Angular hydration does the same job without a mask.

Upstream DSpace has the same gap: provideClientHydration() only in browser-app.config.ts. Upstream issues "Full-screen flash as server-side rendered HTML is re-rendered in the browser" and "Further optimize initial page load by leveraging preboot" describe it. No upstream fix yet. Upstream main also has the MenuEffects reset on REHYDRATE.

Description

The browser now reuses the server-rendered page (Angular hydration) instead of drawing it again. The measured pages no longer jump after load, for anonymous users and for a logged-in admin.

Instructions for Reviewers

List of changes in this PR:

  • provideClientHydration(withNoHttpTransferCache()) moved from browser-app.config.ts to the shared commonAppConfig. Now the server writes the hydration data. Before, the browser found none and silently drew the page from scratch. The HTTP transfer cache is off, because REST data already reaches the browser in the NgRx transfer state.
  • ThemedComponent: the <div #content> wrapper for projected content is now inside @if (!compRef). Before, the code removed it with nativeElement.remove(). Angular did not know that, so the browser looked for the div and hydration failed with NG0500 on <ds-root>.
  • ThemedComponent: a wrapper with projected content gets ngSkipHydration on its host. Angular cannot hydrate DOM nodes passed to createComponent as projectableNodes (the server throws NG0503). Only that wrapper is drawn again in the browser. A static ngSkipHydration set in a template is kept, also when nothing is projected.
  • When the projected content has @if or @for, the parent template also puts a static ngSkipHydration on the wrapper (vanilla file section, CLARIN files field, relation lookup search tab). Angular reads the static attribute to decide whether a block inside the wrapper is hydrated. With the host binding alone, such a block broke when the same template had been rendered once without hydration on that page: NG0500 and 4 file rows instead of 2. The CLARIN item pages do not use these file components, so today this is a safeguard.
  • The three admin grid elements that pass raw DOM nodes as projectableNodes get host: { ngSkipHydration: 'true' }. /admin/ is excluded from SSR by default. Without this, a deployment that renders it on the server would hit NG0503.
  • Home page: the "most used" boxes and the new items box keep their loaded height until their request completes. The browser fills them after load (they are not rendered on the server), and before they pushed the footer down. A failed or empty request gives the space back.
  • MenuEffects is removed. It reset every menu on REHYDRATE, so for a logged-in admin the sidebar disappeared and slid back in after load. The browser adds all menu sections again after REHYDRATE (initPersistentMenus and the route menus run after loadAppState), click handlers included, so the reset is not needed. MenuService.getMenuTopSections() now compares sections by id and visibility, so a section the browser shows after the server hid it (for example after a failed authorization request during SSR) is drawn.
  • server.ts serves /reload/<time>?redirect=<url> (used after login, logout and a language change) with CSR when /reload or the redirect target is excluded from SSR. A relative target is matched from the root, like the router does. Before, excluded pages such as /access-control/bulk-access and /health were rendered on the server after login. Their accordions put a <button> inside the accordion button, the browser moves those nodes, and hydration of the whole page failed (NG0501).
  • withEnabledBlockingInitialNavigation() stays. Angular prints the dev-mode warning NG05001 for it, and its docs say not to combine it with hydration. Hydration works on every page tested.

CLS, anonymous, prod build (Playwright Chromium, 1400x900, cold cache, median of 3-5 loads, REST from the v9 test instance):

page before after
home 0.58 0.002
item 0.71 0.00
full item 0.71 0.00
community 0.61 0.01
login 0.20 0.01
static page 0.49 0.06
licenses 0.65 0.06

Home at 375x812: 0.42 with hydration only, 0.02 with the reserved space. The loaded home page looks the same as before at 14 widths from 375 to 1920 px.

Logged in as admin (local stack, same method, median of 5): home 0.94 → 0.0025, item 1.02 → 0.0004. Without the MenuEffects change it was 0.16 / 0.07. The admin sidebar actions and the item edit menu work, also on the first click after load.

How to test:

  1. npm run build:prod, then npm run serve:ssr.
  2. Open an item page and the home page with DevTools, CPU throttling 4x. The page does not go blank or jump after load.
  3. view-source: shows ngh="..." attributes and an <!--nghm--> comment.
  4. A development build (ng build --configuration development plus ng run dspace-angular:server:development, with ssr.enabled: true) logs Angular hydrated N component(s) and no NG0500-NG0507 errors. If the server asks for a hashed en.<hash>.json, copy en.json to that name.
  • npm run test -- --include=src/app/shared/theme-support/themed.component.spec.ts
  • npm run test -- --include=src/app/home-page/home-page.component.spec.ts
  • npm run test -- --include=src/app/core/core.effects.spec.ts
  • npm run test -- --include=src/app/shared/ssr-path.util.spec.ts
  • npm run test -- --include=src/app/shared/menu/menu.service.spec.ts

Codex review (gpt-6.2-sol, high), 3 rounds: 3 findings fixed (projected @if in a themed wrapper, admin grids with raw projectableNodes, home space kept after an empty or failed request). Not changed in this PR:

  • Person grid cards put a <div> inside a <p>, so a server-rendered grid search would fail to hydrate. /search is not rendered on the server by default (excludePathPatterns, enableSearchComponent: false).
  • The new items box keeps its space if a search hit's item cannot be loaded, and the facet boxes keep theirs if dspace.ui.url cannot be read. Both mean REST is already failing.

A 4th Codex run on the MenuEffects and /reload commits found 3 more issues, all fixed (menu visibility after REHYDRATE, relative redirect target, an explicit /reload exclusion); its other 2 are the REST failure cases above. The nested buttons in the health and bulk access accordions are not changed: both pages are excluded from SSR by default.

Checklist

  • My PR is created against the main branch of code (unless it is a backport or is fixing an issue specific to an older branch).
  • My PR is small in size (e.g. less than 1,000 lines of code, not including comments & specs/tests), or I have provided reasons as to why that's not possible.
  • My PR passes ESLint validation using npm run lint
  • My PR doesn't introduce circular dependencies (verified via npm run check-circ-deps)
  • My PR includes TypeDoc comments for all new (or modified) public methods and classes. It also includes TypeDoc for large or complex private methods.
  • My PR passes all specs/tests and includes new/updated specs or tests based on the Code Testing Guide.
  • My PR aligns with Accessibility guidelines if it makes changes to the user interface.
  • My PR uses i18n (internationalization) keys instead of hardcoded English text, to allow for translations.
  • My PR includes details on how to test it. I've provided clear instructions to reviewers on how to successfully test this fix or feature.
  • If my PR includes new libraries/dependencies (in package.json), I've made sure their licenses align with the DSpace BSD License based on the Licensing of Contributions documentation.
  • If my PR includes new features or configurations, I've provided basic technical documentation in the PR itself.
  • If my PR fixes an issue ticket, I've linked them together.

🤖 Generated with Claude Code

milanmajchrak and others added 9 commits November 26, 2026 16:34
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 <[email protected]>
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 <[email protected]>
Themed wrappers whose projected content has control flow get a static
ngSkipHydration, and the admin grid elements that project raw DOM nodes skip it too.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The host binding removed it when no content was projected. The server then
wrote hydration data for a wrapper that the browser skips.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The reserved height now follows a loading flag that is cleared when the
request completes, not the presence of content.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The browser reset every menu on REHYDRATE. The admin sidebar was then
hidden until the menu providers had checked the rights again, and slid
back in. For a logged-in admin this shifted the whole page.

The server state already holds the built menus. The browser menu
providers replace each section by id, click handlers included. So the
reset is not needed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
After login the app reloads through /reload/<time>?redirect=<url>. Excluded pages were rendered there,
and the nested buttons in the health and bulk access accordions broke hydration of the whole page.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A reload is served by CSR when its own path or its target is excluded. A relative
redirect target is matched from the root, like the router does. The top menu
sections are compared after the visibility filter, so a section the browser shows
after the server hid it is drawn.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sub sections still trigger the top sections again, as before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant