1

The responses to most HTTP get requests are being cached in IE Edge. It doesn't happen in other browsers as far as I know. I fixed that on older versions, but How can I do it on Angular2?

any help would be appreciated.

Sara N
  • 1,079
  • 5
  • 17
  • 45

1 Answers1

0

A common fix is to add a random query parameter value to the request URL. You can create a custom Http implementation that does that for each request.

@Injectable() 
class NoCacheHttp extends Http {
  constructor(_backend: ConnectionBackend, _defaultOptions: RequestOptions) {
    super(_backend, _defaultOptions);
  }

  get(url: string, options?: RequestOptionsArgs) : Observable<Response> {
    let newUrl = /* add some random or incrementing number query parameter to URL */
    return super.get(newUrl, options);
  }

  post(...)
  ...
}
@NgModule({
  imports: [HttpModule],
  export: [HttpModule],
  providers: [{provide: Http, useClass: NoCacheHttp}]
})
export class NoCacheHttpModule {}
@NgModule({
  imports: [BrowserModule, NoCacheHttpModule],
  declarations: [AppModule],
  bootstrap: [AppModule]
})
export class AppModule {}
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • Thank you for the help, I didnt get where should I put these @NgModule s . should the second one be in app.module? and the first one in separate module? Also could you please explain providers: [{Http, useClass: NoCacheHttp}] ? – Sara N Dec 07 '16 at 03:13
  • it seems correct solution. I just have one issue , No provider for ConnectionBackend! in AppComponent – Sara N Dec 07 '16 at 05:44
  • That's weird. If you have `HttpModule` imported, `ConnectionBackend` should be available. I'll try it myself.. – Günter Zöchbauer Dec 07 '16 at 05:47
  • Sorry, I had a few mistakes in my code. This is working https://plnkr.co/edit/N3BUIVt5r3hVVZpK0tTY?p=preview. I had to add the `ConnectionBackend` provider explicitely. I don't know why this is necessary. Perhaps something related to the order how providers are added to the root scope. – Günter Zöchbauer Dec 07 '16 at 05:58