Skip to content

[4.x] Start fetching script modules before the page needs them - #10632

Merged
calebporzio merged 1 commit into
4.xfrom
claude/eager-script-module-delivery
Aug 30, 2026
Merged

calebporzio merged 1 commit into
4.xfrom
claude/eager-script-module-delivery

Conversation

@calebporzio

@calebporzio calebporzio commented Aug 29, 2026 •

Copy link
Copy Markdown
Collaborator

Follow-up to #10631, now rebased directly onto 4.x. That PR makes script-module loading correct — a component's tree suspends until its module has run. This one makes the suspended window invisible by starting fetches as early as the server knows they're coming. Everything here is a pure latency optimization: each component still imports and runs its own module when it mounts, so nothing in this PR is load-bearing — a missed preload just means a slightly longer window, never incorrect behavior.

1. modulepreload links on initial page loads

SupportJsModules::dehydrate() already knows a mounting component's module URL and hash. On full-page renders it now registers

<link rel="modulepreload" href="/livewire/js/audio-uploader.js?v=88fca3e1">

through the existing rendered-assets head injection. The browser's preload scanner starts the fetch while parsing HTML — long before livewire.js boots, discovers the effect, and calls import(). Today's waterfall (parse → boot → discover → fetch) becomes a parallel fetch, and the module is typically fetched and compiled before DOMContentLoaded, so the deferred window collapses to microtasks — resumed before first paint in the common case.

Two extra wins for free:

  • Lazy placeholders skip the scriptModule effect (the real view hasn't rendered), but the page render still knows the module is coming — the placeholder mount emits the preload link, so the module is warm by the time hydration needs it.
  • wire:navigate merges new head assets before initializing the new page, so preload links ride along automatically.

Update requests are unaffected: the rendered-assets bucket is only injected into full-page HTML responses.

2. childScriptModules on update responses

For children mounted by an update there's nothing to preload ahead of time — the response is the discovery. But the server knows which children with modules mounted during the request before the client sees any HTML. Each such mount is collected into a request-scoped static (cleared on flush-state), and the responding component's dehydrate drains it into an effect:

{ "childScriptModules": [{ "name": "testns::alpine-data", "hash": 3939658634 }] }

The client fire-and-forgets import() for each the moment the response arrives — overlapping the fetch with state sync and the morph, instead of starting it only when the child's root is encountered mid-morph. Lazy-hydrating components drain the same way, which also covers the nested case (a module component inside a lazy module component) that would otherwise fetch as a waterfall.

Tests

  • UnitTest.php (6 tests): initial render injects the preload link into the head (asserted through the real RequestHandled injection), no link for module-less components, childScriptModules announced when an update mounts a child and absent when it doesn't, the collector draining so the next update announces nothing, and lazy hydration announcing the modules of the children it mounts.
  • BrowserTest.php: preload link present in the head on an initial render and on a page that only renders a lazy placeholder — both pages still load their modules and log no console errors.

Full SupportJsModules browser suite (17) green on top of the defer branch.

Notes from adversarial review: preload registration explicitly skips Livewire update requests (new children there are covered by childScriptModules, and rendered assets would otherwise ride along in update payloads); and if a module file changes between a lazy placeholder's page render and its hydration, the two hashes produce two fetches — harmless, and inherent to hash-versioned URLs.

🤖 Generated with Claude Code

https://claude.ai/code/session_01VxEoar4u3NVYop2jweewwf

@calebporzio
calebporzio force-pushed the claude/eager-script-module-delivery branch from 4923c9d to 5d7edb8 Compare August 30, 2026 11:50
@calebporzio
calebporzio force-pushed the claude/defer-init-for-script-modules branch from e2eddf0 to e992667 Compare August 30, 2026 12:01
@calebporzio
calebporzio force-pushed the claude/eager-script-module-delivery branch from 5d7edb8 to b132eea Compare August 30, 2026 12:01
Base automatically changed from claude/defer-init-for-script-modules to 4.x August 30, 2026 12:11
Suspending a component while its module loads is correct but leaves a
window where the markup is visible and inert. Shrink that window to
nearly nothing by starting fetches as early as the server knows they're
coming:

- Initial page loads (and lazy placeholders) inject a modulepreload link
  into the head, so the browser fetches the module while parsing HTML —
  long before Livewire boots and discovers it. wire:navigate carries the
  link along in its head merge for free.

- Updates that mount new children announce them in a childScriptModules
  effect on the responding component, and the client starts those
  imports the moment the response arrives — before the morph that adds
  the children to the page.

Both are pure latency optimizations: every component still imports and
runs its own module when it mounts, so nothing here is load-bearing.

Co-Authored-By: Claude Fable 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VxEoar4u3NVYop2jweewwf
@calebporzio
calebporzio force-pushed the claude/eager-script-module-delivery branch from b132eea to bfe96ca Compare August 30, 2026 12:14
@calebporzio
calebporzio merged commit e855004 into 4.x Aug 30, 2026
1 check passed
@calebporzio
calebporzio deleted the claude/eager-script-module-delivery branch August 30, 2026 12:25
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.

1 participant