0

When using plain js ajax, the jsapi google library could be lazy loaded via ajax as follows:

$.ajax({
    url: 'http://www.google.com/jsapi',
    dataType: 'script',
    cache: true, // otherwise will get fresh copy every page load
    success: function() {
      google.load("visualization", "1", {packages:["corechart"]}); 
    }
});

Here you can set the cache: true attribute.

Question: how can I set this attribute in a normal <script> tag?

Problem: I'm lazy loading the library in angularjs as follows on request:

  var script = document.createElement('script');
  script.src = '//www.google.com/jsapi?callback=callback';
  document.body.appendChild(script);

Every time the page is accessed, the library is fetched again from google. It's never cached. Is it possible to enforce caching here?

membersound
  • 81,582
  • 193
  • 585
  • 1,120
  • From http://stackoverflow.com/a/18671689/5378743 i would say your ajax and script tag use the same caching logic. – Roland Starke Apr 21 '16 at 09:46
  • If so: why if `jsapi` js file always loaded new, eg for https://developers.google.com/chart/interactive/docs/quick_start#how-about-a-bar-chart , even as it has `Cache-Control:"private, max-age=3600, must-revalidate"` – membersound Apr 21 '16 at 09:55
  • when i run your script (https://jsfiddle.net/vL8gfsss/) chrome dev tools tell me: jsapi?callback=initCharts (from cache) – Roland Starke Apr 21 '16 at 10:12

0 Answers0