Defer breaks autoload (when loading page without having resources cached)
See original GitHub issueInformation:
- Prism version: 1.17.1
- Plugins: autoloader
- Environment: Browser
Description
When the page is loaded without the JS resources being cached and the scripts are marked with defer
then the syntax highlighting is not applied.
Example
Without defer
(workking): https://jsfiddle.net/f5vj903e/
With defer
(not working): https://jsfiddle.net/f5vj903e/1/
Issue Analytics
- State:
- Created 4 years ago
- Comments:29 (15 by maintainers)
Top Results From Across the Web
How to load from browser cache after the resources are ...
The solution i think off is to callback a .reload() after all the files are cached in the browser so you don't keep...
Read more >Lazy Loading Individual Vue Components and Prefetching
The purpose of lazy loading is to postpone downloading parts of your application that are not needed by the user on the initial...
Read more >Load JavaScript deferred - WP Rocket Knowledge Base
What does Load JavaScript Deferred do? This option addresses the Eliminate Render Blocking Resources PageSpeed recommendation. It is preferable ...
Read more >How To Fix a Slow WordPress Site + Pass Core Web Vitals
Fix your slow WordPress site and pass core web vitals with these 25 in-depth speed optimization tips.
Read more >25 Ways to Speed Up a Slow Elementor Website (2022)
1. Activate Elementor Experiments (optimized DOM output, improved asset + CSS loading, and inline Font icons). This addresses several PageSpeed recommendations ... 2....
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
Alright, I think I know what causes the race condition.
Since the
readyState
of the document isinteractive
, Prism Core usesrequestAnimationFrame
to highlight all code blocks. The problem with this is that this means that all plugins have to loaded and executed before the next frame. This is the reason why some plugins are sometimes active and sometimes not.(You can verify that this is indeed what’s going on by setting a breakpoint in e.g
prism-linenumbers.min.js
and you’ll see that the breakpoint sometimes hits after all code blocks have been highlighted.)This is a bit of an issue because we’ll have to wait for resources that might be loaded. Unfortunately,
defer
doesn’t work on inline scripts:So, instead of using the
requestAnimationFrame
path, we should use theDOMContentLoaded
path in Prism Core becausedefer
red scripts are guaranteed to be executed before that event. And to figure out whether the current script is deferred or not, we needcurrentScript
. (yay, full circle) I’m imagining something along the lines of the following as a fix.Have I verified that this fix works? No. Why not? Funny story. As it turns out: the moment you add an inline script everything works. No race condition, it just works. Srsly, try it. Just add
<script>/* how? */</script>
anywhere in the test page. (btw<script></script>
doesn’t work. Seems like someone at Mozilla wanted to optimize) How’s that as a fix? Just add<script>/**/</script>
.Ok, I just verified that the fix works. I’ll make a PR.