Skip to content

Include the render language in the SSR cache key - #6562

Open
jr-rk wants to merge 1 commit into
DSpace:mainfrom
dataquest-dev:upstream-fix/ssr-cache-key-language
Open

jr-rk wants to merge 1 commit into
DSpace:mainfrom
dataquest-dev:upstream-fix/ssr-cache-key-language

Conversation

@jr-rk

@jr-rk jr-rk commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

References

Description

The in-memory SSR caches in server.ts (the bot cache is on by default) use only req.url as the key. The language of an SSR render comes from the dsLanguage cookie or the Accept-Language header, so crawlers get a page in the language of the first anonymous visitor (human or bot) for up to a day (bot cache timeToLive). With the anonymous cache enabled, other visitors get it too.
The cache key now also contains the language the request is rendered in, e.g. /home|en.

Instructions for Reviewers

List of changes in this PR:

  • server.ts: getCacheKey() now returns <url>|<language> (e.g. /home|en) instead of <url>. The key still starts with the URL, so the /reload exclusion is unchanged.
  • New src/app/core/locale/ssr-cache-key.ts builds the key. The language is resolved the same way SSR picks it for anonymous requests: an active dsLanguage cookie, else the first active Accept-Language entry, else fallbackLanguage. So de and de-DE,de;q=0.9 share one cache entry. It is outside server.ts so Karma can test it.
  • New ssr-cache-key.spec.ts.

Trade-off: the helper copies SSR's language resolution instead of changing it, so this stays a key-only fix; the spec pins the helper's behaviour. Memory stays bounded by max, but on a multilingual site the same max covers fewer distinct URLs. No new configuration.

How to reproduce (default config/config.yml, REST = sandbox.dspace.org, bot cache on by default):

  1. Build and start the server:
    npm ci
    npm run build:prod
    npm run serve:ssr
  2. In a second terminal, request /home as a crawler, first in German, then in English:
    UA='Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)'
    curl -s -A "$UA" -H 'Accept-Language: de' http://localhost:4000/home | grep -o '<html[^>]*>'
    curl -s -A "$UA" -H 'Accept-Language: en' http://localhost:4000/home | grep -o '<html[^>]*>'
  3. Check the lang attribute:
    • Without this PR: lang="de", then lang="de". The English request got the cached German page.
    • With this PR: lang="de", then lang="en".

To run it again, restart the server first (this empties the cache). The cookie -H 'Cookie: dsLanguage=de' instead of Accept-Language: de gives the same result.

Spec: npm test -- --include src/app/core/locale/ssr-cache-key.spec.ts

Affected branches: dspace-10_x, dspace-9_x and dspace-8_x have the same key and the bot cache on by default, so I'd ask for port to labels for those. The change applies cleanly to dspace-10_x. On dspace-9_x and dspace-8_x, the @dspace/* path aliases don't exist (and dspace-8_x uses defaultLanguage instead of fallbackLanguage), so they need small manual ports, which I'm happy to open.

AI Disclosure (REQUIRED)

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.)

Claude Code (Opus 5.5) helped me reproduce the bug on main, write the patch and the spec, and draft this description. I reviewed the change, checked that the helper matches the LocaleService logic, and ran the spec and the before/after reproduction above locally.

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

The server-side page caches (bot cache, enabled by default, and the
anonymous cache) were keyed by req.url only. The language of an SSR render
depends on the dsLanguage cookie and the Accept-Language header, so a page
rendered in one language was served from the cache to visitors who asked
for another one.

The key is now the URL followed by the language the request is rendered in,
resolved the same way LocaleService/ServerLocaleService do for anonymous
requests (active cookie language, then the first active Accept-Language
entry, then the fallback language). Using the resolved language instead of
the raw header keeps header variants that ask for the same language in one
cache entry. The key still starts with the URL, so /reload paths are still
excluded from caching.

The key logic moves to src/app/core/locale/ssr-cache-key.ts so it can be
unit tested.
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