Best way to use Google's hosted jQuery, but fall back to my hosted library on Google fail

前端 未结 23 1375
太阳男子
太阳男子 2020-11-21 07:19

What would be a good way to attempt to load the hosted jQuery at Google (or other Google hosted libs), but load my copy of jQuery if the Google attempt fails?

I\'m n

23条回答
  •  清酒与你
    2020-11-21 07:45

    There are some great solutions here, but I'll like to take it one step further regarding the local file.

    In a scenario when Google does fail, it should load a local source but maybe a physical file on the server isn't necessarily the best option. I bring this up because I'm currently implementing the same solution, only I want to fall back to a local file that gets generated by a data source.

    My reasons for this is that I want to have some piece of mind when it comes to keeping track of what I load from Google vs. what I have on the local server. If I want to change versions, I'll want to keep my local copy synced with what I'm trying to load from Google. In an environment where there are many developers, I think the best approach would be to automate this process so that all one would have to do is change a version number in a configuration file.

    Here's my proposed solution that should work in theory:

    • In an application configuration file, I'll store 3 things: absolute URL for the library, the URL for the JavaScript API, and the version number
    • Write a class which gets the file contents of the library itself (gets the URL from app config), stores it in my datasource with the name and version number
    • Write a handler which pulls my local file out of the db and caches the file until the version number changes.
    • If it does change (in my app config), my class will pull the file contents based on the version number, save it as a new record in my datasource, then the handler will kick in and serve up the new version.

    In theory, if my code is written properly, all I would need to do is change the version number in my app config then viola! You have a fallback solution which is automated, and you don't have to maintain physical files on your server.

    What does everyone think? Maybe this is overkill, but it could be an elegant method of maintaining your AJAX libraries.

    Acorn

提交回复
热议问题