0

I am using $.getScript to load some of my program code. My code looks something like this:

var mainJs = "/main.js";
$.getScript( mainJs )
    .then(function () {
        console.log("main.js loaded");
    });

The code works fine. However, I am thinking to use Angular-cache to do caching control on it. Is it possible to be done?

9
  • Are you trying to prevent it being loaded again or trying to save another trip to server? Commented Aug 26, 2015 at 15:42
  • @charlietfl I want to prevent it from loading again, even by next time user is visiting. It only try to get from server again when the cache is expired by angular-cache. Commented Aug 26, 2015 at 15:46
  • angular cache doesn't store anything for next visit either. i think you have your concepts of caching mixed up a bit. Caching this won;t prevent the script from running again within the same app session....is that your goal? If so you will need some service to keep track if it has been run or not Commented Aug 26, 2015 at 15:53
  • @charlietfl Isn't that angular cache can store as localStorage? Correct me if I am wrong.. Commented Aug 26, 2015 at 15:58
  • no ... that is a different concept entirely. angular $http cache stores actual responses in javascript objects. Those are not maintained between page loads Commented Aug 26, 2015 at 16:01

1 Answer 1

0

I am not sure whether angular is able to interfere the caching of jQuery. But by default $.getScript doesn't cache.

$.getScript is a shortcut of:

$.ajax({ 
    url: '',
    dataType: "script",
    cache: false
});

Reference: http://api.jquery.com/jQuery.getScript/

By default, $.getScript() sets the cache setting to false.

You need to use:

$.ajax({
    url: url,
    cache: true,
    dataType: 'script',
    ...
});
Sign up to request clarification or add additional context in comments.

5 Comments

This doesn't prevent another request to server. It really has no impact at all. The cache of $.ajax is not the same concept as angular cache where data is literally stored in a datastore within page
@charlietfl It does make change. Please check jsfiddle.net/Lttg8ava. Using cache:false makes the response code 200 every time. However, using cache:true makes subsequent response code 304, which means no more new content from server.
OK but concept is still different than angular cache. I really think OP wants to prevent this code running again. I get 200 with cache true also ...opps probably because I have cache disabled
from docs: Note: Setting cache to false will only work correctly with HEAD and GET requests.. The cache ajax does is just add a timestamp in url. So it doesn't even add it for script requests
script requests are GET requests. cache option applies to getScript.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.