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

HTML script tags are async by default #103

Closed
pmarchezz opened this issue Oct 11, 2021 · 7 comments
Closed

HTML script tags are async by default #103

pmarchezz opened this issue Oct 11, 2021 · 7 comments

Comments

@pmarchezz
Copy link

Hi, I'm using Flems for some code examples on a project's documentation. Recently my examples broke, apparently because some script tags added in HTML don't appear to be blocking anymore (the second one executes when the first one is still loading). For example:

<script
  src="https://code.jquery.com/jquery-3.6.0.js"
  integrity="sha256-H+K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk="
  crossorigin="anonymous"></script>
  
<script>
  console.log(window.$); 
</script>

This logs a function in both a test webpage and on editors like jsfiddle or similar. However on Flems it logs undefined (I actually got the expected result once or twice, I assume it's not 100% consistent since it looks like a race condition).

(I'm not saying that this is a reasonable way to use jquery, it's just to provide a minimal case where the issue is reproduced)

I think this is caused by commit 7620dcb4d7c2784a4929d04053b4d0d205044a96 and in particular by this change:

@@ -339,7 +340,7 @@ function create(tag, options) {
   const el = document.createElement(tag)
 
   for (const key in options)
-    el[key] = options[key]
+    options[key] && (el[key] = options[key])
 
   return el
 }

This means the async attribute is not added to the generated script tag anymore when it's false and, since (if I'm not mistaken) all script tags created via js are async by default, it causes script tags to load/execute out of order (which causes the above behaviour). I tried building my own version of Flems with this slight change (key === 'async' || options[key]) && (el[key] = options[key]) and it fixes the issue.

Can you take a look to validate if that's an issue or not? If more info are needed feel free to ask. Thanks!

@osban
Copy link

osban commented Oct 11, 2021

This might explain why I've (recently) noticed that sometimes dependencies (e.g. Mithril) aren't loaded before the page code executes.

@tormodvm
Copy link
Contributor

I've also experienced this problem lately.

@tbreuss
Copy link

tbreuss commented Oct 18, 2021

I've experienced this "timing" issues, too. Quite often in fact.

@porsager
Copy link
Owner

Nice debugging @pmarchezz ! I'll see if I can get that fixed today 👍

@porsager
Copy link
Owner

porsager commented Nov 1, 2021

Fix also pushed to flems.io 🙂

@tbreuss
Copy link

tbreuss commented Nov 1, 2021

Fix also pushed to flems.io 🙂

Does this also affect possible issues when integrating Flems like described here? https://github.com/porsager/flems#getting-started

I had similar "timing" issues that randomly occured.

@porsager
Copy link
Owner

porsager commented Nov 1, 2021

Yeah, if you upgrade to latest that should solve your issues 😊

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

No branches or pull requests

5 participants