Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert async loading of main hljs #120

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

gagbo
Copy link

@gagbo gagbo commented May 3, 2021

Having the main hljs loading asynchronously makes the code blocks bug, since the
page throws hljs is undefined on hljs.initHighlightOnLoad called just after.

Having the main hljs loading asynchronously makes the code blocks bug, since the
page throws hljs is undefined on hljs.initHighlightOnLoad called just after.
@dwy6626
Copy link
Contributor

dwy6626 commented Jun 4, 2021

Just personal opinion. The initHighlightingOnLoad function is deprecated, it may be solved by using highlightAll instead?
https://highlightjs.readthedocs.io/en/latest/api.html#inithighlightingonload

@Tristor
Copy link
Contributor

Tristor commented Jun 9, 2021

Howdy, I made the original change. I'd like to understand this better, as I am using syntax highlighting on my site as well and did not encounter any issues with this change. It's possible there could be an underlying race condition I created by making this load async that doesn't trigger for me, but does for you.

The intention is to not have blocking JS in the head. If at all possible, it's best practice for performance to not have blocking JS and to lazy load or asynchronously load JS imports.

@Szeraax
Copy link

Szeraax commented Feb 16, 2022

So wouldn't the PROPER way to get both satisfied for it to wait until the window is loaded before applying the syntax? Something like:

<script defer src='...'></script>
<script defer src='...'></script>
<script defer>window.onload = (event) => { hljs.highlightAll(); }</script>

That's what I'm using and its working GREAT. Tested on firefox, chrome, and edge. Source material: https://flaviocopes.com/javascript-async-defer/

I found that when I used async for the js files and nothing or defer for the window.onload() script, it would sometimes work and sometimes not work. It would seem that if all three of them are set to defer, it will make sure to run them in order after the HTML has run through the parser. Anyone able to confirm if I'm crazy or not? #NotADev

Unrelated, can someone explain to me why the URIs are missing the https: infront of the //.../?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants