How to disable caching with HttpClient get in Angular 6

后端 未结 4 1153
感动是毒
感动是毒 2020-12-09 16:52

I\'m writing an Angular SPA app, that uses HttpClient to get values from my backend.

What is the easy way to tell it not to cache? The first time I ask it gets the v

相关标签:
4条回答
  • 2020-12-09 17:08

    How about add salt to url:

    const salt = (new Date()).getTime();
    return this.httpClient.get(`${url}?${salt}`, { responseType: 'text' });
    

    Same concept is used for static resource links in html (css or js) to trick the cache. Adding dynamic salt to url causes fresh loading of target each time because url is different every time, but in fact it is the same.

    /static/some-file.css?{some-random-symbols}

    I used date because it guarantee me unique number without using random and so on. We can just use incrementing integer for each call as well.

    The code provided above worked fine for me in case when I couldn't change server configuration.

    0 讨论(0)
  • 2020-12-09 17:09

    HTTPInterceptors are great way to modify HTTP requests occurring in your application. It acts as an injectable service that can invoke when HttpRequest occurs.

    HTTP Interceptor:

    import { Injectable } from '@angular/core';
    import { HttpInterceptor, HttpRequest, HttpHandler, HttpHeaders } from '@angular/common/http';
    
    @Injectable()
    export class CacheInterceptor implements HttpInterceptor {
    
      intercept(req: HttpRequest<any>, next: HttpHandler) {
        const httpRequest = req.clone({
          headers: new HttpHeaders({
            'Cache-Control': 'no-cache',
            'Pragma': 'no-cache',
            'Expires': 'Sat, 01 Jan 2000 00:00:00 GMT'
          })
        });
    
        return next.handle(httpRequest);
      }
    }
    

    Using Interceptor:

    import { NgModule } from '@angular/core';
    import { BrowserModule } from '@angular/platform-browser';
    import { FormsModule } from '@angular/forms';
    
    import { AppComponent } from './app.component';
    
    import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
    import { CacheInterceptor } from './http-interceptors/cache-interceptor';
    
    @NgModule({
      imports:      [ BrowserModule, FormsModule ],
      declarations: [ AppComponent ],
      bootstrap:    [ AppComponent ],
      providers: [
        { provide: HTTP_INTERCEPTORS, useClass: CacheInterceptor, multi: true }
      ]
    })
    export class AppModule { }
    
    0 讨论(0)
  • 2020-12-09 17:18

    Using meta HTML tags, Disable browser caching:-

    <meta http-equiv="cache-control" content="no-cache, must-revalidate, post-check=0, pre-check=0">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="pragma" content="no-cache">
    

    or,

    Add headers in http request as:-

    headers = new Headers({
            'Cache-Control':  'no-cache, no-store, must-revalidate, post- 
                                check=0, pre-check=0',
            'Pragma': 'no-cache',
            'Expires': '0'
        });
    
    0 讨论(0)
  • 2020-12-09 17:18

    As answered by Pramod, you can use http request interceptor to modify or set a new header on the request. Below is a much simpler way of setting headers on http request interceptor for Later angular versions(Angular 4+). This approach would only set or update a certain request header. This is to avoid removing or overriding some important headers like the authorization header.

    // cache-interceptor.service.ts
    import { Injectable } from '@angular/core';
    import {
      HttpInterceptor,
      HttpRequest,
      HttpHandler,
    } from '@angular/common/http';
    
    @Injectable()
    export class CacheInterceptor implements HttpInterceptor {
    
      intercept(req: HttpRequest<any>, next: HttpHandler) {
        const httpRequest = req.clone({
          headers: req.headers
            .set('Cache-Control', 'no-cache')
            .set('Pragma', 'no-cache')
            .set('Expires', 'Sat, 01 Jan 2000 00:00:00 GMT')
        })
    
        return next.handle(httpRequest)
      }
    }
    
    // app.module.ts
    
      import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http'
      import { CacheInterceptor } from './cache-interceptor.service';
    
      // on providers
      providers: [{ provide: HTTP_INTERCEPTORS, useClass: CacheInterceptor, multi: true }]
    
    0 讨论(0)
提交回复
热议问题