defer external script parsing until after DOMContentLoaded#666
Open
andreahlert wants to merge 1 commit intobigskysoftware:masterfrom
Open
defer external script parsing until after DOMContentLoaded#666andreahlert wants to merge 1 commit intobigskysoftware:masterfrom
andreahlert wants to merge 1 commit intobigskysoftware:masterfrom
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 calladdCommand.The root cause is in
browserInit(): external.hsfiles 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 runsaddCommand("breakpoint", ...), the parser has already choked on it.This isn't just an hdb problem. Any plugin that registers commands or features via
addCommand/addFeatureis affected.template.jswith itsrendercommand 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:loadlistener registration to fire immediately insideready()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 ifbrowserInitwere called more than once.Includes a few tests in
test/core/pluginLoading.jsto verify commands and features registered viaaddCommand/addFeatureare available to subsequently parsed scripts.Closes #533