Skip to content

Fix false-positive "Missing records in staticData" warning in useTolgeeSSR#3518

Closed
jangjunha wants to merge 1 commit into
tolgee:mainfrom
jangjunha:fix-missing-records-warning
Closed

Fix false-positive "Missing records in staticData" warning in useTolgeeSSR#3518
jangjunha wants to merge 1 commit into
tolgee:mainfrom
jangjunha:fix-missing-records-warning

Conversation

@jangjunha
Copy link
Copy Markdown

@jangjunha jangjunha commented Apr 17, 2026

Summary

useTolgeeSSR logs Tolgee: Missing records in "staticData" for proper SSR functionality: "<ns>:<lang>" even when the required records are present in staticData. SSR itself works correctly — only the warning is wrong.

Root cause

The check compares two differently-ordered keys:

  • Required records are stringified inline as `${namespace}:${language}` (e.g. "web:en").
  • Provided records come from tolgee.getAllRecords(), whose cacheKey is produced by encodeCacheKey in @tolgee/core as `${language}:${namespace}` (e.g. "en:web").

Because the two are inverted, the .filter(...) never finds a match and every required record is reported as missing.

Summary by CodeRabbit

  • Bug Fixes
    • Fixed server-side rendering to correctly identify missing records when namespace configuration is present.

The SSR missing-records warning built required keys as `namespace:language`,
while `getAllRecords()` returns `cacheKey` in `language:namespace` order.
The filter therefore never matched and the warning fired even when
`staticData` was provided correctly.

Swap the order so both sides use `language:namespace`.
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 17, 2026

Walkthrough

The pull request modifies cache key formatting in SSR initialization across two files. When a namespace is present, the key construction order is reversed from namespace:language to language:namespace for detecting missing translation records.

Changes

Cohort / File(s) Summary
SSR Cache Key Format Update
packages/react/src/useTolgeeSSR.ts, packages/vue/src/TolgeeProvider.ts
Reversed the cache key composition order: changed from namespace:language to language:namespace when namespace is present. This affects how missing records are identified and compared against provided SSR data.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 A rabbit's ode to the key reordering dance,
Language first, then namespace—a reversed happenstance!
Cache keys now twist in a new configuration,
SSR records align for proper translation. 🔑✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically identifies the main change: fixing a false-positive warning in useTolgeeSSR related to missing records in staticData.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
packages/react/src/useTolgeeSSR.ts (1)

69-78: Fix looks correct; consider guarding the warning against an empty missingRecords.

The swap to ${language}:${namespace} aligns with encodeCacheKey in packages/core/src/Controller/Cache/helpers.ts, so the comparison against r?.cacheKey will now match as intended.

One small edge case: console.warn is still emitted whenever !tolgee.isLoaded(), even if missingRecords ends up empty (e.g. when the instance is not yet loaded for reasons unrelated to static data). The message then prints an empty list, which is misleading. Consider only warning when missingRecords.length > 0.

Proposed tweak
-      // eslint-disable-next-line no-console
-      console.warn(
-        `Tolgee: Missing records in "staticData" for proper SSR functionality: ${missingRecords.map((key) => `"${key}"`).join(', ')}`
-      );
+      if (missingRecords.length > 0) {
+        // eslint-disable-next-line no-console
+        console.warn(
+          `Tolgee: Missing records in "staticData" for proper SSR functionality: ${missingRecords.map((key) => `"${key}"`).join(', ')}`
+        );
+      }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/react/src/useTolgeeSSR.ts` around lines 69 - 78, The warning is
emitted even when missingRecords is empty; change the code around missingRecords
and the console.warn so the warning only runs when missingRecords.length > 0
(i.e., wrap the existing console.warn call in an if (missingRecords.length > 0)
block). Keep using the computed keys that match encodeCacheKey and the existing
providedRecords lookup; no other logic changes are needed. Ensure this check
sits next to the existing tolgee.isLoaded() usage so the message only appears
when there are actually missing staticData entries.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@packages/react/src/useTolgeeSSR.ts`:
- Around line 69-78: The warning is emitted even when missingRecords is empty;
change the code around missingRecords and the console.warn so the warning only
runs when missingRecords.length > 0 (i.e., wrap the existing console.warn call
in an if (missingRecords.length > 0) block). Keep using the computed keys that
match encodeCacheKey and the existing providedRecords lookup; no other logic
changes are needed. Ensure this check sits next to the existing
tolgee.isLoaded() usage so the message only appears when there are actually
missing staticData entries.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 5e118520-57aa-41a3-8224-ebd6547289c2

📥 Commits

Reviewing files that changed from the base of the PR and between 23591f6 and 213893a.

📒 Files selected for processing (2)
  • packages/react/src/useTolgeeSSR.ts
  • packages/vue/src/TolgeeProvider.ts

@Anty0
Copy link
Copy Markdown
Member

Anty0 commented Jun 3, 2026

Superseded by #3524

@Anty0 Anty0 closed this Jun 3, 2026
JanCizmar pushed a commit that referenced this pull request Jun 5, 2026
…3524)

## Problem

`useTolgeeSSR` (React) and `TolgeeProvider` (Vue) log

> `Tolgee: Missing records in "staticData" for proper SSR functionality:
"<ns>:<lang>"`

even when the required records **are** present in `staticData`. SSR
works correctly — the warning is simply wrong, which sends users
debugging a non-existent problem.

The two sites built the comparison key inline as ``
`${namespace}:${language}` ``, but every cache key in `@tolgee/core` is
produced by `encodeCacheKey` as `` `${language}:${namespace}` ``.
Because the orders are inverted, the lookup never matched and every
record was reported missing.

This duplicated, hand-rolled encoding is the root cause: the format rule
lived in three independent copies, and the two SSR copies silently
drifted from the canonical one.

## Solution

- Export `encodeCacheKey` from `@tolgee/core` (it already flows to
`@tolgee/web` via `export *`) and reuse it at both SSR sites instead of
re-deriving the key format. This removes the duplication so the orders
can no longer drift apart.
- Emit the warning only when records are actually missing — a sufficient
`staticData` set previously still logged an empty `…functionality: `
message (reachable in Vue via lazy/function-valued static data).
- Add regression tests: both packages cover the false-positive (a
provided namespaced record must not be reported missing), and Vue
additionally covers the empty-list guard via function-valued static
data. The expected key is derived from `encodeCacheKey` so the tests
catch any future drift on either side.

## Out of scope

In React the missing-records check compares against the cache
(`getAllRecords()`), which never stores function-valued (lazy) static
data, so a React app that supplies *all* required namespaces as lazy
loaders would still see a false warning. This is pre-existing behaviour
on a different axis (lazy vs. eager) and is left for a separate change;
the React empty-records guard here is therefore defensive only, since
missing-records and the loaded check derive from the same cache.

Supersedes #3518 (external PR; reuses the canonical encoder rather than
only correcting the literals, and adds tests).

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

* **New Features**
  * Exposed encodeCacheKey in the core package API.

* **Bug Fixes**
* Improved SSR missing-records detection consistency by standardizing
cache key encoding across React and Vue.

* **Tests**
* Added integration tests covering SSR missing-records warning behavior
in React and Vue.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
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.

2 participants