0

Im loading a javascript file from a CDN. Im developing locally and I noticed that when my internet went down and the remote file wasn't available, the entire page failed to load.

Is there a way of linking to a remote file, but making it so the rest of the page will load if this file is missing? Thanks

2 Answers 2

1

You could use asynchronous loading. That way unavailable files will not block loading the rest of the page but you could of course still encounter unexpected behavior if some necessary script files fail to load.

Sign up to request clarification or add additional context in comments.

Comments

0

You can also try placing the JavaScript file at the bottom of the page or use conditional includes.

if(Request.IsLocal)
{
    // Use local files
}
else
{
    // Use CDN files
}

6 Comments

Im only working locally during development. Once the site is live no requests to it will be local. Im most interested in best practice here, I know the Google CDN is unlikely to go down but id still like to have a backup in place, and I also may want to use less reliable CDNs. Thanks
I have been using the conditional includes because when working for instance with my laptop outdoors or at a remote site I may not have access to the internet thereby rendering my CDNs useless. Once deployed the conditional check is negligible especially once you take caching into consideration.
You can try conditional include using #if DEBUG which will also work but not get compile into the assembly when building in Release mode, etc.
Sorry, but what does that do? Ive googled it quickly but didnt understand what I read. Thanks
Whenever your code is compiled in debug mode (the DEBUG symbol is created by the compiler when the application is run in debug mode) if will execute the code within the #if DEBUG ... #endif block, however when the code is compile in another mode, for instance release, then the code within the #if DEBUG ... #endif block is not compiled into the assembly.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.