What\'s the best practice of using Twitter Bootstrap, refer to it from CDN or make a local copy on my server?
Since Bootstrap keeps evolving, I am afraid if I refer
Why Not Both ¯\_(ツ)_/¯ ? Scott Hanselman has a great article on using a CDN for performance gains but gracefully falling back to a local copy in case the CDN is down.
Specific to bootstrap, you can do the following to load from a CDN with a local fallback:
.visible
class instead of testing for rgb(51, 51, 51)
.hidden
and .d-none
for either Boostrap 3.x or 4 display:none
, and then conditionally insert a stylesheet using js.To your question on Best Practices, there are a lot of very good reasons to use a CDN in a production environment:
- It increases the parallelism available.
- It increases the chance that there will be a cache-hit.
- It ensures that the payload will be as small as possible.
- It reduces the amount of bandwidth used by your server.
- It ensures that the user will get a geographically close response.
To your versioning concern, any CDN worth its weight in salt with let you target a specific version of the library so you don't accidentally introduce breaking changes with each release.
document.write
According to the mdn on document.write
Note: as
document.write
writes to the document stream, callingdocument.write
on a closed (loaded) document automatically callsdocument.open
, which will clear the document.
However, the usage here is intentional. The code needs to be executed before the DOM is fully loaded and also in the correct order. If jQuery fails, we need to inject it into the document inline before we attempt to load bootstrap, which relies on jQuery.
HTML Output After Load:
In both of these instances though, we're calling while the document is still open so it should inline the contents, rather than replacing the entire document. If you're waiting till the end, you'll have to replace with document.body.appendChild
to insert dynamic sources.
Aside: In MVC 6, you can do this with link and script tag helpers