Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(transloco): prevent loading translations when injector is destroyed #835

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions libs/transloco/src/lib/transloco.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export class TranslocoService {
};

private destroyRef = inject(DestroyRef);
private destroyed = false;

constructor(
@Optional() @Inject(TRANSLOCO_LOADER) private loader: TranslocoLoader,
Expand Down Expand Up @@ -151,6 +152,7 @@ export class TranslocoService {
});

this.destroyRef.onDestroy(() => {
this.destroyed = true;
// Complete subjects to release observers if users forget to unsubscribe manually.
// This is important in server-side rendering.
this.lang.complete();
Expand Down Expand Up @@ -200,6 +202,13 @@ export class TranslocoService {
}

load(path: string, options: LoadOptions = {}): Observable<Translation> {
// If the application is already destroyed, the `load` should not
// execute anything in practice because other resources have already
// been released and destroyed.
if (this.destroyed) {
return EMPTY;
}

const cached = this.cache.get(path);
if (cached) {
return cached;
Expand Down