Module: Dommy::Js::ScriptBoot

Defined in:
lib/dommy/js/script_boot.rb

Overview

Boot a parsed document's <script> tags like a browser: run them in two passes that mirror the HTML spec, set document.currentScript around each, and replay the readyState lifecycle so ready-gated startup code (Stimulus / Turbo / jQuery ready) takes the real path.

loading -> parser-blocking classic scripts (document order)
      -> deferred scripts: modules + classic `defer` (document order)
      -> interactive (DOMContentLoaded)
      -> complete (load)

The two passes matter: a <script type="module"> is deferred — it must run after the parser-inserted classic scripts even when it appears earlier in the document (e.g. a Nuxt entry module placed above the inline window.__NUXT__ = {...} bootstrap it depends on). Running everything in one document-order pass would execute the module against half-initialized globals. A failed fetch or a throwing script is isolated; on_error is notified (the Browser collects it for strict mode, the Capybara adapter ignores it) so the rest of the page still loads. Shared by Dommy::Browser and the Capybara driver so script boot lives in one place.

The module is the stable entry point; the work lives on ScriptBooter, a short-lived instance that holds the runtime / document / resources / on_error collaborators so they aren't threaded through every step.

Class Method Summary collapse

Class Method Details

.run_document_scripts(runtime, document, resources: nil, on_error: nil, on_script: nil) ⇒ Object



31
32
33
# File 'lib/dommy/js/script_boot.rb', line 31

def run_document_scripts(runtime, document, resources: nil, on_error: nil, on_script: nil)
  ScriptBooter.new(runtime, document, resources: resources, on_error: on_error, on_script: on_script).run
end

.run_external_script(runtime, document, element, src, resources: nil, on_error: nil) ⇒ Object

Fetch + execute a single <script src> that was dynamically inserted into an already-booted document (webpack/Vite on-demand chunk loading), then fire its load / error event so the loader's promise settles.



38
39
40
# File 'lib/dommy/js/script_boot.rb', line 38

def run_external_script(runtime, document, element, src, resources: nil, on_error: nil)
  ScriptBooter.new(runtime, document, resources: resources, on_error: on_error).run_inserted_external(element, src)
end