Skip to content

defer external script parsing until after DOMContentLoaded#666

Open
andreahlert wants to merge 1 commit intobigskysoftware:masterfrom
andreahlert:fix/deferred-plugin-loading
Open

defer external script parsing until after DOMContentLoaded#666
andreahlert wants to merge 1 commit intobigskysoftware:masterfrom
andreahlert:fix/deferred-plugin-loading

Conversation

@andreahlert
Copy link
Copy Markdown

Right now if you have a behavior file loaded via <script type="text/hyperscript" src="..."> and that file uses a command from a plugin like hdb.js (e.g. breakpoint), it blows up with "Unexpected Token" because the behavior file gets parsed before the plugin has a chance to call addCommand.

The root cause is in browserInit(): external .hs files are fetched and parsed in a .then() chain that can resolve before synchronous <script> tags further down the page have executed. So by the time hdb.js runs addCommand("breakpoint", ...), the parser has already choked on it.

This isn't just an hdb problem. Any plugin that registers commands or features via addCommand/addFeature is affected. template.js with its render command has the same issue.

The fix is small: external files are still fetched immediately (no performance hit), but parsing is deferred into the ready() callback. This guarantees every synchronous plugin <script> in the page has already executed by the time behavior files are parsed.

Also moved the htmx:load listener registration to fire immediately inside ready() instead of waiting for the fetch promise, so dynamic htmx swaps that happen while external files are still loading don't get silently dropped.

Added { once: true } to the DOMContentLoaded listener to prevent a leak if browserInit were called more than once.

Includes a few tests in test/core/pluginLoading.js to verify commands and features registered via addCommand/addFeature are available to subsequently parsed scripts.

Closes #533

Fixes bigskysoftware#533. External behavior files loaded via
<script type="text/hyperscript" src="..."> were being parsed before
plugins like hdb.js could register their commands, causing
"Unexpected Token: breakpoint" errors.

The fix separates fetch from parse: external files are still fetched
immediately for performance, but parsing is deferred until after
ready() fires, giving synchronous plugin scripts time to register.

Signed-off-by: André Ahlert <andre@aex.partners>
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.

Debugger dependency order

1 participant