Welcome to WebmasterWorld Guest from 23.23.53.177

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Google API fallback to local version using rewrite?

   
2:50 pm on Nov 17, 2009 (gmt 0)

5+ Year Member



When using Google API to deliver things like jQuery I would like to fall back to a local version if Google for some reason fails to deliver (I know it is unlikely, but I like to be safe).
I could do this by testing the existence of the jQuery object in javascript, but I was wondering if it could also be done using rewrites?

I have this:


<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>

And a "local" file in the relative path: js/jquery.js

I would like the "local" file to be loaded if the "remote" file is unavailable.

2:58 pm on Nov 17, 2009 (gmt 0)

WebmasterWorld Senior Member themadscientist is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



There's not any simple way I can think of with with mod_rewrite, because the browser makes the request for the script from the remote site, not through your server...

To do what you are asking I would probably use PHP or some other scripting language to 'check' for the file when the page loads and dynamically switch the URL served to the browser if it's not present or there's an issue.

3:01 pm on Nov 17, 2009 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



You could write a server-side script in PERL or PHP to do this, if it won't matter that all requests for ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js will be logged as coming from your server instead of from the client.

[clarity] The client-side ajax call would then be modified to go to your server which would then make a request to google, and if that request timed out or otherwise failed, return the local content. [/clarity]

Jim

3:37 pm on Nov 17, 2009 (gmt 0)

WebmasterWorld Senior Member themadscientist is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



Yeah, I was thinking about just putting the request to see if the files was readable on the pages and switching the URL served to the client based on the result, but jdMorgan's way might be better, because you can use the some request for the jQuery script all the time and just switch where the contents of the file are served from based on the result of the attempted connection...

Either way, in PHP I would probably use is_readable() to make the determination and remember if you go with jdMorgan's you will either have to parse JS as php (or whatever scripting language you decide on) or set the jQuery file location as .ext (php, pl, etc.) to get the script to run before the content is delivered.

4:58 pm on Nov 17, 2009 (gmt 0)

5+ Year Member



Ok, thanks for the help. I guess I stay with the pure javascript solution then. I was just wondering if it could be done with a mod_rewrite.
10:25 am on Nov 26, 2009 (gmt 0)

5+ Year Member



Keeping in mind that this rewrite stuff just doesn't seem to agree with me what I am about to say is probably stupid.
But why wouldn't this work: (it is sort of in pseudo-code but you get the general idea?)

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js$ ^js/jquery.js$

4:01 pm on Nov 26, 2009 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



For two reasons:

1) RewriteRule cannot 'see' the protocol or domain name -- It looks at only the localized requested URL-path. You'd need to use RewriteConds to examine those other parts of the request.

2) Unless you own and control "googleapis.com", then your code can have no effect, because the query.min.js script is requested by the client (e.g. browser) from that server, not from yours.

Jim

 

Featured Threads

Hot Threads This Week

Hot Threads This Month