Skip to content

Commit 052aa36

Browse files
committed
fix(transloco): prevent loading translations when injector is destroyed
1 parent 345181e commit 052aa36

File tree

2 files changed

+12
-252
lines changed

2 files changed

+12
-252
lines changed

libs/transloco/src/lib/transloco.service.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ export class TranslocoService {
109109
};
110110

111111
private destroyRef = inject(DestroyRef);
112+
private destroyed = false;
112113

113114
constructor(
114115
@Optional() @Inject(TRANSLOCO_LOADER) private loader: TranslocoLoader,
@@ -144,6 +145,7 @@ export class TranslocoService {
144145
});
145146

146147
this.destroyRef.onDestroy(() => {
148+
this.destroyed = true;
147149
// Complete subjects to release observers if users forget to unsubscribe manually.
148150
// This is important in server-side rendering.
149151
this.lang.complete();
@@ -192,7 +194,15 @@ export class TranslocoService {
192194
return this.availableLangs;
193195
}
194196

195-
load(path: string, options: LoadOptions = {}): Observable<Translation> {
197+
load(path: string, options: LoadOptions = {}): Observable<Translation> {
198+
// If the application has already been destroyed, return an empty observable.
199+
// We use EMPTY instead of NEVER to ensure the observable completes.
200+
// This is important for operators like switchMap, which rely on the inner observable completing
201+
// before they can subscribe to the next one. NEVER would hang the chain indefinitely.
202+
if (this.destroyed) {
203+
return EMPTY;
204+
}
205+
196206
const cached = this.cache.get(path);
197207
if (cached) {
198208
return cached;

package-lock.json

Lines changed: 1 addition & 251 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)