Skip to content

JCU/perf(ssr): answer handle redirects without SSR - #1701

Open
vidiecan wants to merge 2 commits into
customer/jcufrom
jcu/handle-redirect-without-ssr
Open

vidiecan wants to merge 2 commits into
customer/jcufrom
jcu/handle-redirect-without-ssr

Conversation

@vidiecan

@vidiecan vidiecan commented Nov 2, 2026 •

Copy link
Copy Markdown

Problem

Part of https://github.com/dataquest-dev/dspace-customers/issues/1121.

/handle/<prefix>/<suffix> is rendered by Angular SSR only to send a 301 to the object's page (0.4–1 s of Node CPU per hit on dspace.jcu.cz). On 2026-11-02 a scraper sent tens of thousands of these and saturated Node; the backend was not the bottleneck. HEAD on a handle URL also returns 200 instead of 301 (ngApp renders only GET; upstream DSpace#4042).

Change

An Express route in server.ts, before the catch-all render, for GET/HEAD /handle/<prefix>/<suffix>[/] (src/backend/handle-redirect.ts):

  • one anonymous Discovery lookup (/api/discover/search/objects?query=handle:"<handle>", 1 s timeout, no cookies or auth forwarded, redirects not followed);
  • answers 301 with a relative Location, like DsoRedirectService: /communities/<uuid>, /collections/<uuid>, /items/<uuid>, or /entities/<type>/<uuid> when the item has a visible dspace.entity.type; ui.nameSpace is prefixed and the query is dropped, as today;
  • Discovery is used instead of pid/find because pid/find returns only a REST URL and cannot tell /items from /entities;
  • logged-in requests (auth cookie or Authorization), no or ambiguous results, other types, deeper paths, errors and timeouts fall through to Angular unchanged;
  • the 301 carries cache.control (max-age=604800), which the Angular redirect gets today through addCacheControl (every handle contains a dot);
  • failed lookups (non-OK, errors, timeouts) are logged with console.warn at most once per minute, with the count, so a broken setup is visible; a missing or invalid timeoutMs falls back to 1000 ms;
  • switch: ssr.handleRedirect.enabled (default true, DSPACE_SSR_HANDLEREDIRECT_ENABLED=false disables), ssr.handleRedirect.timeoutMs (1000).

Tests

  • npm run test:handle-redirect: 50 passed (item, community, collection, entity type, trailing slash, query, HEAD, namespace, unknown/ambiguous, timeout/error fall-through, logged-in, switch off, Cache-Control, throttled warnings, invalid timeoutMs, body release).
  • npm run test:ssr-cache-stats: 7 passed.
  • npx tsc --project tsconfig.server.json --noEmit: passed.
  • npm run lint: passed (existing warnings); eslint src/backend/handle-redirect.ts: clean.
  • npm run build:prod: passed on the first commit; not rerun after the review fixes (server-only change, tsc passes).

Not done: a load test, and a check against the live backend for restricted or withdrawn items (they fall through to Angular by design).

Review

Scrutiny reviews (Claude Fable, Mistral): no correctness or security findings in the redirect itself. Fixed in the second commit: missing Cache-Control on the 301, silent failures, invalid timeoutMs, body not released after a failed parse.

Alternative

The same redirect can be answered by nginx with pid/find (no FE image needed); that variant is described in https://github.com/dataquest-dev/dspace-customers/issues/1121.

jm added 2 commits November 3, 2026 00:12
…tomers#1121)

- 301 carries cache.control (max-age=604800), as the Angular redirect does today via addCacheControl
- failed lookups (non-OK, errors, timeouts) are warned at most once per minute, with the count
- missing or invalid timeoutMs falls back to 1000 ms instead of aborting every lookup
- a body left unread by a failed JSON parse or abort is cancelled
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