Fix false-positive "Missing records in staticData" warning in useTolgeeSSR#3518
Fix false-positive "Missing records in staticData" warning in useTolgeeSSR#3518jangjunha wants to merge 1 commit into
Conversation
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`.
WalkthroughThe pull request modifies cache key formatting in SSR initialization across two files. When a namespace is present, the key construction order is reversed from Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
packages/react/src/useTolgeeSSR.ts (1)
69-78: Fix looks correct; consider guarding the warning against an emptymissingRecords.The swap to
${language}:${namespace}aligns withencodeCacheKeyinpackages/core/src/Controller/Cache/helpers.ts, so the comparison againstr?.cacheKeywill now match as intended.One small edge case:
console.warnis still emitted whenever!tolgee.isLoaded(), even ifmissingRecordsends 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 whenmissingRecords.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
📒 Files selected for processing (2)
packages/react/src/useTolgeeSSR.tspackages/vue/src/TolgeeProvider.ts
|
Superseded by #3524 |
…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 -->
Summary
useTolgeeSSRlogsTolgee: Missing records in "staticData" for proper SSR functionality: "<ns>:<lang>"even when the required records are present instaticData. SSR itself works correctly — only the warning is wrong.Root cause
The check compares two differently-ordered keys:
`${namespace}:${language}`(e.g."web:en").tolgee.getAllRecords(), whosecacheKeyis produced byencodeCacheKeyin@tolgee/coreas`${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