Skip to content

fix(transloco): avoid duplicate fallback lang request when already cached#912

Open
arturovt wants to merge 1 commit into
masterfrom
fix/issue-625
Open

fix(transloco): avoid duplicate fallback lang request when already cached#912
arturovt wants to merge 1 commit into
masterfrom
fix/issue-625

Conversation

@arturovt

@arturovt arturovt commented Apr 6, 2026

Copy link
Copy Markdown
Collaborator

When useFallbackTranslation is enabled, loading a secondary language (e.g. 'es') would internally call getFallbacksLoaders, which invokes resolveLoader directly — bypassing the service cache. This caused a duplicate HTTP request for the fallback lang whenever it had already been fetched (e.g. as the default lang).

Fix: before delegating to getFallbacksLoaders, check whether the fallback lang's observable is already in the cache. If it is, reuse it via a mapped forkJoin instead of issuing a new network request.

Adds a regression test covering the case where fallbackLang matches defaultLang and a second language is loaded afterward.

Closes #625

PR Checklist

PR Type

  • Bugfix

Does this PR introduce a breaking change?

  • No

Summary by CodeRabbit

Release Notes

  • Bug Fixes

    • Improved translation loading performance by preventing redundant loader calls when fallback languages are already cached.
  • Tests

    • Added test coverage for fallback language caching behavior.

…ched

When `useFallbackTranslation` is enabled, loading a secondary language
(e.g. 'es') would internally call `getFallbacksLoaders`, which invokes
`resolveLoader` directly — bypassing the service cache. This caused a
duplicate HTTP request for the fallback lang whenever it had already been
fetched (e.g. as the default lang).

Fix: before delegating to `getFallbacksLoaders`, check whether the
fallback lang's observable is already in the cache. If it is, reuse it
via a mapped `forkJoin` instead of issuing a new network request.

Adds a regression test covering the case where `fallbackLang` matches
`defaultLang` and a second language is loaded afterward.
@coderabbitai

coderabbitai Bot commented Apr 6, 2026

Copy link
Copy Markdown

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: be9440a9-a661-4795-aa52-b40bb7f74085

📥 Commits

Reviewing files that changed from the base of the PR and between bd284ec and 65aa194.

📒 Files selected for processing (2)
  • libs/transloco/src/lib/tests/service/missingHandler.spec.ts
  • libs/transloco/src/lib/transloco.service.ts

📝 Walkthrough

Walkthrough

Modified TranslocoService.load to check the cache before constructing fallback loaders. When a fallback translation already exists in cache, it reuses the cached observable instead of calling getFallbacksLoaders. A test case was added to verify this optimization prevents duplicate loader invocations.

Changes

Cohort / File(s) Summary
Fallback Translation Cache Optimization
libs/transloco/src/lib/transloco.service.ts, libs/transloco/src/lib/tests/service/missingHandler.spec.ts
Implemented cache check in TranslocoService.load to prevent redundant loader calls when fallback translations are already cached. Uses forkJoin with cached observable when available, otherwise falls back to prior getFallbacksLoaders behavior. Added test case to verify no extra loader invocations occur when loading additional active languages with a cached fallback.

Suggested reviewers

  • shaharkazaz

Fun fact: Did you know? In i18n, fallback languages follow a cascade pattern—if a translation isn't available in the requested language, the system gracefully falls back to alternatives. This is why many apps default to English as the ultimate fallback. In Portuguese, the word "idioma" means language!

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main fix: avoiding duplicate fallback language requests when the translation is already cached.
Description check ✅ Passed The PR description includes the problem statement, solution, test coverage, and addresses all required checklist items from the template.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

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

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/issue-625

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.

@pkg-pr-new

pkg-pr-new Bot commented Apr 6, 2026

Copy link
Copy Markdown

Open in StackBlitz

@jsverse/transloco

npm i https://pkg.pr.new/jsverse/transloco/@jsverse/transloco@912

@jsverse/transloco-locale

npm i https://pkg.pr.new/jsverse/transloco/@jsverse/transloco-locale@912

@jsverse/transloco-messageformat

npm i https://pkg.pr.new/jsverse/transloco/@jsverse/transloco-messageformat@912

@jsverse/transloco-optimize

npm i https://pkg.pr.new/jsverse/transloco/@jsverse/transloco-optimize@912

@jsverse/transloco-persist-lang

npm i https://pkg.pr.new/jsverse/transloco/@jsverse/transloco-persist-lang@912

@jsverse/transloco-persist-translations

npm i https://pkg.pr.new/jsverse/transloco/@jsverse/transloco-persist-translations@912

@jsverse/transloco-preload-langs

npm i https://pkg.pr.new/jsverse/transloco/@jsverse/transloco-preload-langs@912

@jsverse/transloco-schematics

npm i https://pkg.pr.new/jsverse/transloco/@jsverse/transloco-schematics@912

@jsverse/transloco-scoped-libs

npm i https://pkg.pr.new/jsverse/transloco/@jsverse/transloco-scoped-libs@912

@jsverse/transloco-utils

npm i https://pkg.pr.new/jsverse/transloco/@jsverse/transloco-utils@912

@jsverse/transloco-validator

npm i https://pkg.pr.new/jsverse/transloco/@jsverse/transloco-validator@912

commit: 65aa194

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

No issues found across 2 files

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.

Bug(transloco): transloco loader trigger 2 times on scoped translation

2 participants