Angular httpResource not reloading for signal in interceptor

Discussão em 'Angular' iniciado por hankor, Abril 13, 2025.

  1. hankor

    hankor Guest

    I have a service with a signal for a selected language:

    export class LanguageService {
    languages = ['en-US', 'de-DE'];
    language = signal('en-US'); //Signal for the selected language
    }


    I read this signal in an (httpClient) interceptor, that is attaching the selected language to the http header of requests:

    export function languageInterceptor(req: HttpRequest<unknown>, next: HttpHandlerFn): Observable<HttpEvent<unknown>> {
    const newReq = req.clone({
    headers: req.headers.append(
    'Accept-Language',
    inject(LanguageService).language() //Interceptor reads signal
    ),
    });
    return next(newReq);
    }


    When using an httpResource it is not reloaded, when the selected language changes:

    resource = httpResource<any>('doesnotreload.json');


    I can only make it work when explicitly calling the language signal:

    resourceThatReloads = httpResource<any>(() => {
    this.languageService.language(); //resource only reloads using this dummy call to language signal
    return 'reloads.json';
    });


    This means signals read in interceptors don't seem to be tracked whereas if I do the same access directly in the resource, they are.

    Is this something that is supposed to work? Can I somehow avoid the dummy call in each httpResource?

    Here's a stackblitz showcase

    Continue reading...

Compartilhe esta Página