Streaming jquery(JS files) from a CDN (Google)

前端 未结 5 1288
说谎
说谎 2020-12-19 13:15

This one is a case of not doing your homework.:-)

Apart from dynamic loading advantage, does it make sense to include a JavaScript library(jQuery in my case ) from

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-19 13:18

    In my opinion, there isn't any real downside to using a well-known jQuery CDN (such as Google's), and there are some potential upsides, so my personal preference is to take the CDN approach. If nothing else, that's one less file for me to have to worry about and maintain as part of my source tree.

    There are several advantages I see to using a CDN vs. a single fat JavaScript archive:

    • Reducing latency for retrieving uncached resources, since these assets are served from edge locations
    • For 3rd-party scripts, the user's browser probably already has them cached and won't have to download them again for your site.
    • Working around per-host browser limitations for parallel requests, by distributing your assets across multiple hostnames (of course, this can also be done via your own servers using simple subdomain tricks).
    • Reducing overall load on your primary servers, esp. important during peak usage periods or YC/Slashdot "attacks".

    When to care about download time for HTML page assets:

    • When you have a significant number of first-time visitors (from search engines and/or social media). Research shows that people aren't very patient these days, so you really need to go out of your way to make a good first impression. (See also Jakob Nielsen's articles on Page Abandonment, Answer Engines and Response Times.)
    • For visitors using mobile devices. Mobile data networks are often slow and/or congested.
    • For shops practicing continuous delivery and causing frequent churn in assets (more common for brand-new sites and apps)

    P.S. - Aaron Hopkins has one of the best write-ups on optimizing page load time I've seen. A bit dated, but still quite relevant.

    P.S.S. - You might want to take a look at Zepto, a light-weight (24K vs 100K), api-compatible alternative to jQuery.

提交回复
热议问题