Jquery getScript caching

前端 未结 6 941
花落未央
花落未央 2020-12-02 20:48

By Default $.getScript() disables caching and you can use $.ajaxSetup and set caching to true. When testing if the script is actually cached with Firebug most of the time t

6条回答
  •  执念已碎
    2020-12-02 20:54

    I know this is an old post, and the existing answer is the real answer, but touching on Iscariot's concern IT REALLY IS CACHING (at least kinda sorta). This is just a quirk of firefox. Maybe this will prove useful to others who are confused by this quirk.

    I tested this concept with a REALLY LARGE javascript file that defines google map polygons for the Idaho DOT district boundaries based on arrays of tens of thousands of latlons (the uncompressed file size is 2,806,257, but I run it through a compression process). Using the following javascript

    // Grab polys if not already loaded
    if (typeof(defaults.data.polys) === 'undefined') {
        /*$.getScript('/Scripts/ScriptMaster.php?file=Districts', function () {});*/
        $.ajax({
            type: "GET",
            url: '/Scripts/ScriptMaster.php?file=Districts',
            success: function() {
                defaults.data.polys = getPolys();
                data.polys = defaults.data.polys;
            },
            dataType: "script",
            cache: true
        });
    }
    

    and you can see the relevant php (you don't want the actual Districts.js file it would take too much space on this post, so here's ScriptMaster.php)

    
    

    It's important to note that calling php's header functions BEFORE the script even executes the string you're going to print as unlike chrome and possibly (probably, I'm just too lazy to check) other browsers firefox appears to make a ping to server to check for headers before using cache. Maybe with more research you could determine if this pertains to elements in as equally as it does with ajax (probably not).

    So I did five test runs showing the load times for this script with ajax as stated in firebug. Here are the results

    #results loading the script after clearing cache (yes those are seconds, not ms)
    200 OK      4.89s
    200 OK      4.9s
    200 OK      5.11s
    200 OK      5.78s
    200 OK      5.14s
    
    #results loading the page with control+r
    200 OK      101ms
    200 OK      214ms
    200 OK      24ms
    200 OK      196ms
    200 OK      99ms
    200 OK      109ms
    
    #results loading the page again by navigating (not refreshing)
    200 OK      18ms
    200 OK      222ms
    200 OK      117ms
    200 OK      204ms
    200 OK      19ms
    200 OK      20ms
    

    As you can see, my localhost server to web client connection is not the most consistent and my laptop specs are a little shabby (single core processor and all, it's a few years old too) BUT THE POINT is there is a significant drop in load time after the cache is loaded.

    [Also in case anyone's curious without the compression script (not like tabs, spaces or new lines are wasted, it just has to be readable still) takes somewhere between 7-8 seconds to load, but I'm not going to do that five times]

    So never fear, it really is caching. For smaller scripts that only take ms's to load you may not notice the difference in firefox, honestly; simply because it checks for headers from the server. I know this because of the load time change from moving those header functions from the end of the script to the start. If you have those functions after php goes through the string it takes longer to load.

    Hope this helps!

提交回复
热议问题