question: script variations; defer and async?
See original GitHub issueCould someone provide a little more explanation of what the prism.js
script does and where we may place it?
The official instructions say to load it at the end of the document using <script src="prism.js"></script>
. But most modern browsers now seem to support async
and defer
when used in <head>
, according to an answer on Stack Overflow. According to one explanation with diagrams, we should be able to load the script in <head>
and just use defer
to have it load asynchronously yet execute after the document.
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Scripts: async, defer - The Modern JavaScript Tutorial
The async attribute is somewhat like defer . It also makes the script non-blocking. But it has important differences in the behavior.
Read more >javascript - Script Tag - async & defer - Stack Overflow
The difference between async and defer centers around when the script is executed. Each async script executes at the first opportunity after it...
Read more >script async vs script defer - this vs that
The async and defer scripts are executed at different moments. After an async script is downloaded, the browser will pause the document parser,...
Read more >What is the difference between async and defer in script ...
The async attribute This ensures that, when the script is encountered, parsing doesn't pause right away. Instead, the script is loaded in the ......
Read more >async vs defer in script tag in JavaScript - Codedamn
The async and defer attributes both allow the browser to continue parsing the HTML document while JavaScript files are being downloaded, but ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
You may want to use
defer
for performance reasons, but that depends on the particulars of your setup. Leaving outdefer
means it pauses while the script is downloaded and executed. Adding it allows the browser to continue parsing & rendering and delay the script execution until that process has completed.It’s worth testing that on your site to see what’s fastest for you, but Prism should function correctly in all scenarios, as long as all Prism scripts have the same attributes, e.g. if you have Prism core + the autoloader, either both or neither should use
defer
.You can generally place Prism’s
<script>
s anywhere you like. If Prism detects that the page is currently loading, it will delay the highlighting until the DOM is ready, so it doesn’t really matter when Prism is loaded/its script is executed. The highlighting itself is done via thePrism.highlightAll
method.The only restriction to your total freedom is plugins/languages in separate files. In that case, you’ll have to use
defer
or the no-attribute version of both<script>
s because with multiple files, order matters.But if you only have one Prism file, then you should be able to place this anywhere you like and in any way you like, I think. (I can’t think of a configuration which doesn’t work at least.)