Skip to content

[4.x] Defer Alpine init until component modules load - #10619

Closed
calebporzio wants to merge 3 commits into
4.xfrom
codex/defer-alpine-component-modules
Closed

calebporzio wants to merge 3 commits into
4.xfrom
codex/defer-alpine-component-modules

Conversation

@calebporzio

Copy link
Copy Markdown
Collaborator

Rebased and reorganized replacement for #10295. The core approach and original regression coverage were developed with @joshhanley, who is credited on each commit.

Problem

  • What does the user experience? A single- or multi-file component registers Alpine.data() in its co-located script module, but Alpine evaluates the component's x-data first. The browser logs Alpine Expression Error: ... is not defined, and the dependent UI is blank or non-interactive. This can happen on initial render, dynamic mounts, lazy hydration, islands, and wire:navigate.
  • What did they expect instead? A component's own script module should finish registering its Alpine data provider before Alpine evaluates directives in that component's DOM.
  • What caused the difference? The scriptModule effect starts an asynchronous import(), while Alpine continues its synchronous start()/initTree() traversal. The async registration loses that race.

Solution

Keep Livewire.start() synchronous and defer Alpine initialization only for a component whose script module is pending.

The trade-off is a brief uninitialized state for only the affected component while its module downloads. That is narrower than making Livewire's global boot and message lifecycles asynchronous.

Alternatives considered

  • Minimum: stop after Level 1. Tiny and useful, but incomplete for already-initialized roots such as lazy hydration.
  • Maximum: add a global async preparation phase that discovers and preloads every component module before Alpine boot, morph, navigation, and island processing. More uniform, but it turns broad lifecycle APIs async and increases the blast radius.
  • Evaporative: compile or hoist component registrations into an eagerly available bundle so runtime component-module imports no longer race Alpine at all. Architecturally clean, but it changes the component compilation and asset model.
  • Userland: move Alpine.data() into a global @assets/alpine:init script or use an inline x-data object. This avoids a framework contribution, but sacrifices co-location and does not make component-local modules correctly ordered.

Verification

  • DUSK_SERVE_PORT=8011 vendor/bin/phpunit --configuration phpunit.xml.dist src/Features/SupportJsModules/BrowserTest.php — 13 tests, 46 assertions
  • npm run test:run — 110 passed, 1 skipped
  • npm run build — both Livewire bundles built successfully
  • Real Laravel app comparison: affected 4.x rendered a blank status with two Alpine reference errors; this branch renders Alpine data loaded with no console errors

Context: #10295, #9861, discussions #9591, #9737, and #9830.

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