Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

171
Visualizações
Angular - How to cache an image indefinetly, regardless of the server's response headers

I have an angular application on my own domain, that displays images from google storage like this:

<img src="googleUrl..."/>

The default Cache-Control is set to 3600 seconds which is way too low for me, because I want to cache the images for 1 week (the images on the storage change very rarely).

I tried to create an HttpInterceptor but it does not intercept the request that the src of the img makes.

I also tried to create a pipe and set a Cache-Control header but, the server being google, it gives me a Cross-Origin error. This is how I implemented it:

@Pipe({
    name: 'cacheResource',
})
export class CacheResourcePipe implements PipeTransform {
    constructor(private http: HttpClient, private sanitizer: DomSanitizer) {}
    transform(url): Observable<SafeUrl> {
        const headers = new HttpHeaders({
            'Cache-Control': 'public, max-age=2.592.000',
        });
        return this.http
            .get(url, {
                headers,
                responseType: 'blob',
            })
            .pipe(map((val) => URL.createObjectURL(val)));
    }
}

And then called like this:

<img [src]="'googleUrl.com' | cacheResource | async" />

(The pipe approach works with files stored on the same domain but I don't have this possibility)

Is there any way to achieve the long cache that I need without changing the server?

about 4 years ago · Santiago Gelvez
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda