Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent IE caching AngularJS/Restangular

In IE, when I make an update to or destroy an item in a list, the action is successful, but the updated list/data is not returned from the server, thus the list is not being updated in the view.

I've tried the following to no avail:

angular.module('casemanagerApp')
.config(function($stateProvider, $urlRouterProvider, RestangularProvider) {

  if (!RestangularProvider.setDefaultHeaders) {
    RestangularProvider.setDefaultHeaders({});
  }

  RestangularProvider.setDefaultHeaders({'If-Modified-Since': 'Mon, 26 Jul 1997 05:00:00 GMT'});
  RestangularProvider.setDefaultHeaders({'Cache-Control': 'no-cache'});
  RestangularProvider.setDefaultHeaders({'Pragma': 'no-cache'});

  $stateProvider

  ...
});

Can anyone point me in the right direction for a solution to this?

like image 1000
sharpmachine Avatar asked Mar 14 '23 22:03

sharpmachine


1 Answers

I would recommend configuring $httpProvider instead of RestangularProvider since Restangular uses $http behind the scenes:

angular.module('casemanagerApp')
.config(function($stateProvider, $urlRouterProvider, $httpProvider) {
  if (!$httpProvider.defaults.headers.get) {
    $httpProvider.defaults.headers.get = {};
  }

  $httpProvider.defaults.headers.get['If-Modified-Since'] = 'Mon, 26 Jul 1997 05:00:00 GMT';      
  $httpProvider.defaults.headers.get['Cache-Control'] = 'no-cache';
  $httpProvider.defaults.headers.get.Pragma = 'no-cache';
  //...

});

EDIT

I just read through the restangular.js code and the setDefaultHeaders function looks like it doesn't append more headers but override the default headers:

object.setDefaultHeaders = function(headers) {
  config.defaultHeaders = headers;
  object.defaultHeaders = config.defaultHeaders;
  return this;
};

This means that you were only setting the 'Pragma' header, which may not be enough to prevent caching.

You could try doing the following:

RestangularProvider.setDefaultHeaders({
    'If-Modified-Since': 'Mon, 26 Jul 1997 05:00:00 GMT',
    'Cache-Control': 'no-cache',
    'Pragma': 'no-cache'
});

This way only Restangular calls will add the headers, and regular http calls won't.

like image 118
yvesmancera Avatar answered Mar 23 '23 15:03

yvesmancera