How to load CDN if available, if not load local script?

Recently a CDN we used went down, and our site malfunctioned.

Is there a way, using script, we can use the CDN if it's available ( if we don't get a 404 for example ), and if it's not, use a local version of our script?


John S.Web DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
You may take a look like solutions like

But the simple approach: test for a variable declared by your scripts. When they don't exist, load your local version.
David S.Commented:
There's also a simpler way. Here's an example for loading jQuery.
<script src=""></script>
<script>if(typeof jQuery=='undefined') document.write(unescape(
"%3Cscript src='/assets/js/jquery-3.3.1.min.js'%3E%3C/script%3E"));</script>

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
David FavorLinux/LXD/WordPress/Hosting SavantCommented:
Or you can simply self host all scripts.

If your site caching is correctly setup then server load will be minimal.

If you're running HTTP/2, then all resources begin serving at same time, so the entire supposed purpose of CDNs only makes sense if you're running the old HTTP/1.1 protocol.

If you self host + your site dies... well nothing lost...

If you use a CDN, even with a fallback strategy, to verify this is working will require you continually test your fallback.

The way you do testing, depend on your code. Likely test method is to set all CDN hosts/domains to in your local caching DNS setup.

This will force all CDN references to return 404s for all assets + cause your fallback mechanism to exercise.

If you use a fallback approach, be sure + test it every day (continual mechanical test), to ensure your fallback mechanism is working all the time.
I never (rarely) use external file for security reason.
If you do so make sure that the link have  integrity="sha...."

for example:
<link href="" rel="stylesheet" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">

Open in new window

For more information
John S.Web DeveloperAuthor Commented:
Thank you all. Very helpful.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.