Skip to content

Do not embed i18n message catalog in TransferState of SSR pages - #6348

Open
saschaszott wants to merge 9 commits into
DSpace:mainfrom
saschaszott:saschaszott-patch-30
Open

saschaszott wants to merge 9 commits into
DSpace:mainfrom
saschaszott:saschaszott-patch-30

Conversation

@saschaszott

Copy link
Copy Markdown
Member

References

Description

Removes the complete i18 message catalog from the SSR TransferState. Instead, the server adds a <link rel="preload" …> element for the content-hashed translation file, and the browser waits for it to be loaded before application bootstrapping. Hanging requests and network timeouts are handled properly and can never block the app bootstrap.

Impact: SSR responses shrink by ~300 kB per page. The translation file is requested once and then served from the browser cache, as its name contains a content hash.

Instructions for Reviewers

List of changes in this PR

  • TranslateServerLoader no longer writes the i18n message catalog to the TransferState. Instead, it adds <link rel="preload" as="fetch" crossorigin="anonymous" href="assets/i18n/<lang>.<hash>.json"> to the document head (right after <base>, so the relative URL resolves correctly)
  • TranslateBrowserLoader always loads the messages via HttpClient
  • New helper getTranslationFileName(), so server (preload link) and browser (HTTP request) use the same name of the i18n translation file
  • BrowserInitService waits in the app initializer until the message catalog for the current language is loaded (avoids message keys briefly showing up when the client renders)
  • Loading starts right after the i18n initialization and is awaited together with the authentication check, so both run in parallel
  • The wait is capped at 5 seconds (TRANSLATIONS_TIMEOUT_MS), so a hanging request (e.g. fetching the authenticated user's language, or the translation file itself) can never block the app. A log message is printed. The app bootstraps anyway and the translation messages are applied once they're loaded.
  • removed the unused NGX_TRANSLATE_STATE key (ngx-translate-state.ts)

How to test

  1. start app in prod mode (with SSR enabled)
  2. open an item landing page (or another page that is rendered server-side) and view page source:
    • NGX_TRANSLATE_STATE is no longer present in <script id="dspace-angular-state">
    • the <head> element contains the preload link for assets/i18n/<lang>.<hash>.json
  3. In the browser's network tab: the translation JSON file is requested exactly once, early during page load, with no "preloaded but not used" warning in the console
  4. With network throttling enabled, reload the page: no message keys (e.g. nav.login) should flash before the translated texts appear
  5. Switch the language and verify that the translations load correctly
  6. Block the request for assets/i18n/<lang>.<hash>.json in the browser's dev tools (Network → "Block request URL") and reload: after at most 5 seconds the app becomes interactive and "Failed to load translation message catalog" is logged to the console

AI Disclosure (REQUIRED)

This PR was created with the assistance of Claude (LLM Opus 5.5, medium effort). Claude was used to better understand and analyze the problem and to assist in the implementation process of this PR. All changes were reviewed in detail and tested by me.

Were AI tools used in the creation of this PR?

  • No (This PR does NOT include AI-generated or AI-assisted content that requires disclosure.)
  • Yes (This PR includes AI-generated or AI-assisted content that requires disclosure.)

Checklist

This checklist provides a reminder of what we are going to look for when reviewing your PR. You do not need to complete this checklist prior creating your PR (draft PRs are always welcome).
However, reviewers may request that you complete any actions in this list if you have not done so. If you are unsure about an item in the checklist, don't hesitate to ask. We're here to help!

  • 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 follows all coding best practices based on the Code Conventions Guide
  • [ x 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.

@tdonohue tdonohue added improvement i18n / l10n Internationalisation and localisation, related to message catalogs performance / caching Related to performance, caching or embedded objects labels Oct 9, 2026
@tdonohue
tdonohue requested a review from nwoodward October 29, 2026 15:41

@nwoodward nwoodward left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @saschaszott! The code looks good, and I confirmed that it behaves as described with a <link rel="preload" …> link in the <head> section instead of loading NGX_TRANSLATE_STATE in the TransferState.

One question, are the changes in .github/workflows/docker.yml part of the PR?

@saschaszott

Copy link
Copy Markdown
Member Author

One question, are the changes in .github/workflows/docker.yml part of the PR?

Yes, because the old check relies on a translation value ("DSpace software", including the double quotes) that is no longer present in the SSR HTML output.

@nwoodward

Copy link
Copy Markdown
Contributor

OK, I see now. Thanks for clarifying that.

@github-project-automation github-project-automation Bot moved this from 🙋 Needs Reviewers Assigned to 👍 Reviewer Approved in DSpace 11.0 Release Nov 4, 2026
@tdonohue tdonohue moved this from 👍 Reviewer Approved to 🎉 Merge Ready in DSpace 11.0 Release Nov 6, 2026
@tdonohue tdonohue added port to dspace-10_x This PR needs to be ported to `dspace-10_x` branch for next bug-fix release port to dspace-9_x This PR needs to be ported to `dspace-9_x` branch for next bug-fix release labels Nov 12, 2026
@github-actions

Copy link
Copy Markdown

Hi @saschaszott,
Conflicts have been detected against the base branch.
Please resolve these conflicts as soon as you can. Thanks!

@saschaszott

Copy link
Copy Markdown
Member Author

In the meantime, the changes from PR #5653 have been merged into main. I need to better understand these changes before I can resolve the new merge conflict in this PR.

@saschaszott

Copy link
Copy Markdown
Member Author

@kanasznagyzoltan , @alanorth : I'm trying to resolve the current merge conflict in this PR. The goal of this PR is to remove the full i18n message catalog from the SSR transfer state. Is this still possible given the changes introduced by the merged PR #5653?

@saschaszott

Copy link
Copy Markdown
Member Author

@tdonohue, since we need to integrate the changes from this PR with those introduced in #5653 (which is a new feature in DS11), I think it will be difficult to backport these changes to older DS versions if we resolve the merge conflict in this PR.

Would it be a better approach to change the target branch of this PR to https://github.com/DSpace/dspace-angular/tree/dspace-10_x and create a new PR that would allow us to easily integrate the changes into main?

@kanasznagyzoltan

Copy link
Copy Markdown
Contributor

@saschaszott yes, I think removing the i18n catalog from the TransferState in #6348 is still possible, and it shouldn't be much work.

#5653 already has a full client-side path: when there's nothing in the TransferState, the browser loader fetches the base file plus the override files of the active theme chain and merges them. The build records which themes ship a file for which language (process.env.themeLanguages), so only existing files are requested. With #6348 the browser would simply always take that path. On the server, readThemeOverrides already resolves the active theme chain and checks which override files exist, so the preload links for those files could be added there, next to the one for the base file.

Two things to keep in mind:

Retargeting #6348 to dspace-10_x sounds reasonable to me, it applies there without conflicts. I'm happy to help with or review the main version of #6348.

@tdonohue

Copy link
Copy Markdown
Member

Would it be a better approach to change the target branch of this PR to https://github.com/DSpace/dspace-angular/tree/dspace-10_x and create a new PR that would allow us to easily integrate the changes into main?

@saschaszott : Yes, it sounds like we should have separate PRs for 10.x (and below) and main. So, that seems reasonable to me. Keep in mind, we'd therefore need to test/approve both PRs before merging either...as we don't want the dspace-10_x and main branches to be out-of-sync with regards to this fix.

@kanasznagyzoltan

Copy link
Copy Markdown
Contributor

@saschaszott I've opened the follow-up for the theme file hashes: #6543.
The main version of #6348 can use the same themeLanguageHashes map for the preload links of the theme files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

i18n / l10n Internationalisation and localisation, related to message catalogs improvement merge conflict performance / caching Related to performance, caching or embedded objects port to dspace-9_x This PR needs to be ported to `dspace-9_x` branch for next bug-fix release port to dspace-10_x This PR needs to be ported to `dspace-10_x` branch for next bug-fix release

Projects

Status: 🎉 Merge Ready

Development

Successfully merging this pull request may close these issues.

SSR pages embed the complete i18n message catalog in TransferState

4 participants