12
In How I should work on Bootstrap and Javascript links?, about how to serve the media files, whether via CDN server or local server, it was answered that it is interesting to keep both: initially loading the file from CDN and, in case of failure, having a fallback on the local server, as it is done with jQuery:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="/js/jquery-1.10.2.min.js"><\/script>')</script>
With Javascript files, create this fallback is trivial, because it is enough to verify the existence of the object, as in window.jQuery || ...
, but with other files such as CSS and images, this is not so trivial.
So how is it possible to define a fallback for importing CSS files?
The idea would be to have as a result something similar to:
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script>bootstrapLoaded || document.write('<link rel="stylesheet" href="assets/css/bootstrap/3.3.7/css/bootstrap.min.css">')</script>
In the case of Bootstrap particularly you can do the same with jQuery, only searching for some method used in Bootstrap JS. If this Bootstrap JS fails, probably the CSS CDN will also fail, ai vc already calls the failed JS and CSS too! This option is not exclusively for qq CSS, but works for Bootstrap and other frameworks that have JS and CSS in the same CDN. If it is not clear tell me that cake an answer with more details.
– hugocsl
Which browsers do you want to support? The option to load all scripts dynamically is valid or do you even want fallbacks for static HTML scripts?
– Sergio
@Sergio dynamic loading is valid. If there are pros/cons, it would be nice to list them.
– Woss