2

What I am trying to accomplish


I will be hosting a web application on a LAN for about 50 people at the maximum. No one from outside the LAN will be accessing the web server. Right now I have the web application using CDNs to deliver Bootstrap and jQuery amongst a few other things. I would like to have the web application fallback to local static copies of the CDN files in case something goes wrong.


My question


  1. If my web server is only being used by users on a LAN, which method is better: a CDN or local files?
  2. How do I fallback on local files without using document.write()?

NOTE: I'm not trying to accomplish this only for jQuery. I would like to do this for several files.


Related SO questions


I have looked at the dozens of other questions that answer what I'm trying to do, the most popular are listed below.
  1. Best way to use Google's hosted jQuery, but fall back to my hosted library on Google fail
  2. How to load local script files as fallback in cases where CDN are blocked/unavailable?
  3. Is Google’s CDN for jQuery available in China?


What I have attempted


I implemented the following code from question (1)
<!--<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>-->
 <script>
     console.log(window.jQuery) // ==> undefined
     window.jQuery || document.write('<script type="text/javascript" src="{% static "js/jquery-3.4.1.min.js" %}"><\/script>');
     console.log(window.jQuery) // ==> undefined
 </script>

but this solution:

  1. fails to load the document
  2. causes Google Chrome DevTools to state the following
[Violation] Avoid using document.write(). https://developers.google.com/web/updates/2016/08/removing-document-write
[Violation] Parser was blocked due to document.write(<script>)

This then led me to the SO question titled A Parser-blocking, cross-origin script is invoked via document.write - how to circumvent it?. I then tried to implement the accepted solution and appended async to my code like so

<script>
   console.log(window.jQuery) // ==> undefined
   window.jQuery || document.write('<script type="text/javascript" src="{% static "js/jquery-3.4.1.min.js" %}" async><\/script>');
   console.log(window.jQuery) // ==> undefined
</script>

but it still fails to load the document and I get the following error again:

[Violation] Avoid using document.write(). https://developers.google.com/web/updates/2016/08/removing-document-write.


In conclusion


So what is the right way to accomplish this? How do I do this without using document.write()? It seems like the answers to the previous questions are now outdated.
Hunter
  • 646
  • 1
  • 6
  • 23
  • Dont use the CDN if its on the lan it won't matter and would be faster serving then any cdn anyway :/ – Lawrence Cherone Jul 03 '19 at 00:32
  • If you already have an internal site, why not just host the libraries internally too? Then you don't have to worry about an external CDN or try to implement something that checks to see if the CDN scripts loaded. – Herohtar Jul 03 '19 at 00:43
  • @Herohtar I could definitely do that, I just wasn't sure if I should be using a CDN or not really. I was using a CDN just for development purposes but now that I'm about to deploy it to production I was trying to make sure this thing is as stable as possible (ie. doesn't break and has redundancies), hence wanting to have a fallback plan. – Hunter Jul 03 '19 at 00:49
  • 1
    For something internal a CDN doesn't really make sense, especially so with a small number of users. That makes the site dependent on an external resource, as you have realized. If you host them internally you are guaranteed that they will be available as long as your internal server is available, and if your internal server isn't working the other components of your site wouldn't be available either. – Herohtar Jul 03 '19 at 00:52

1 Answers1

3

You should listen to the onerror event, if it occurs then load the local one.
Here is an example (you should change the code to fit your case):

<script>
    function onJqueryLoadError()
    {
        document.write('<scr' + 'ipt src="static/jquery-3.4.1.min.js"></scr' + 'ipt>');
    }
</script>
<script src="https://www.cdn/jquery-3.4.1.min.js" onerror="onJqueryLoadError()"></script>
Pinetree
  • 617
  • 4
  • 7
  • Ok so I set it up like you have and then I removed a couple characters from the CDN URL to cause an error, and it still failed to load jQuery. I got the same error as I did in my original post – Hunter Jul 03 '19 at 01:10
  • The code which I post works fine on my test. Could you paste the error here? – Pinetree Jul 03 '19 at 01:34