Skip to content

Use Alpine deferred initialization for component script modules - #10623

Closed
calebporzio wants to merge 2 commits into
4.xfrom
codex/use-alpine-deferred-init
Closed

calebporzio wants to merge 2 commits into
4.xfrom
codex/use-alpine-deferred-init

Conversation

@calebporzio

Copy link
Copy Markdown
Collaborator

Problem

  • What users experience: Alpine expressions inside a Livewire component can fail during initial render, lazy hydration, morphing, islands, or navigation when they reference Alpine.data() or $js actions registered by that component's <script> module. The browser reports that the registration is undefined, or a directive sees Livewire's temporary Promise proxy instead of the loaded action.
  • What users expected: A component-local script module should finish registering its Alpine data and JavaScript actions before Alpine initializes that component's DOM.
  • Why reality differs: Component script modules are dynamic imports, while Alpine initializes DOM trees synchronously. Livewire had a way to observe a pending asset, but Alpine had no supported way to suspend only the affected tree. The earlier patch worked around that gap by toggling private Alpine ignore/marker state and manually re-running initTree().

Solution

  • Level 1: consume Alpine's deferred-init boundary — passes the module promise to Alpine.deferInit() and clears Livewire's pending-module state before Alpine resumes. This removes the need to manipulate _x_ignore, _x_marker, clone interceptors, or call initTree() manually.
  • Level 2: prove the full lifecycle matrix — adds browser coverage for initial, lazy, dynamic, forwarded-slot morph, island, wire:navigate, navigation persistence, $js readiness, and failed-import behavior.

This is the balanced path: Alpine owns Alpine initialization, while Livewire only declares the promise its component depends on.

Dependency / release order

Draft until alpinejs/alpine#4886 is merged, released, and Livewire's Alpine lock is updated to that release. A clean install of Alpine 3.16.3 does not contain deferInit() yet.

This is intended as the fundamental successor to #10619; that PR should remain available for comparison until this dependency is resolved.

Verification

  • npm run build
  • Focused Livewire browser tests: 9 passing, 42 assertions
  • Real Laravel application reproduction: component-local Alpine.data() renders successfully
  • Level 1 also builds independently

@calebporzio
calebporzio deleted the codex/use-alpine-deferred-init branch August 30, 2026 12:14
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