Skip to content

Commit 649364c

Browse files
authored
Fix Livewire component premature registration (#1390)
REF: livewire/livewire#7076 (comment) In the latest release of Livewire, Laravel Service Container is utilized for Dependency Resolution, which after testing with a new installation of Jetstream found to have a premature component registration due to a race condition. **Explaination:** - Previously, mechanisms were created using standard PHP object creation `(new $mechanism)`, not involving Laravel's service container, hence no service container events were triggered. `LivewireServiceProvider::registerMechanisms()` - With the recent change, these mechanisms are now instantiated via Laravel's IoC service container `app($mechanism)`, causing service container events to be triggered. - 💡 **The core of the problem** arises when the `Livewire\Mechanisms\CompileLivewireTags` mechanism is instantiated. It extends `Illuminate\View\Compilers\ComponentTagCompiler` which has a constructor dependency of `Illuminate\View\Compilers\BladeCompiler` , and its creation via the service container triggers events. On the other hand, **the `JetstreamServiceProvider` prematurely listens to this event in the service provider `register()` method**, leading to a situation where Livewire components are being registered before all necessary mechanisms are set up, particularly the `Livewire\Mechanisms\ComponentRegistry` which comes next in order after `Livewire\Mechanisms\CompileLivewireTags`. **Suggested fix:** - Move the registration of Livewire components in `JetstreamServiceProvider` to the `boot()` method, which is where it should be. This ensures all mechanisms are in place before any component registration begins. It also makes the additional event handling for BladeCompiler resolution unnecessary. `$this->callAfterResolving(BladeCompiler::class, fn () => '');`
1 parent 5b445d2 commit 649364c

File tree

1 file changed

+20
-22
lines changed

1 file changed

+20
-22
lines changed

src/JetstreamServiceProvider.php

+20-22
Original file line numberDiff line numberDiff line change
@@ -35,28 +35,6 @@ class JetstreamServiceProvider extends ServiceProvider
3535
public function register()
3636
{
3737
$this->mergeConfigFrom(__DIR__.'/../config/jetstream.php', 'jetstream');
38-
39-
$this->callAfterResolving(BladeCompiler::class, function () {
40-
if (config('jetstream.stack') === 'livewire' && class_exists(Livewire::class)) {
41-
Livewire::component('navigation-menu', NavigationMenu::class);
42-
Livewire::component('profile.update-profile-information-form', UpdateProfileInformationForm::class);
43-
Livewire::component('profile.update-password-form', UpdatePasswordForm::class);
44-
Livewire::component('profile.two-factor-authentication-form', TwoFactorAuthenticationForm::class);
45-
Livewire::component('profile.logout-other-browser-sessions-form', LogoutOtherBrowserSessionsForm::class);
46-
Livewire::component('profile.delete-user-form', DeleteUserForm::class);
47-
48-
if (Features::hasApiFeatures()) {
49-
Livewire::component('api.api-token-manager', ApiTokenManager::class);
50-
}
51-
52-
if (Features::hasTeamFeatures()) {
53-
Livewire::component('teams.create-team-form', CreateTeamForm::class);
54-
Livewire::component('teams.update-team-name-form', UpdateTeamNameForm::class);
55-
Livewire::component('teams.team-member-manager', TeamMemberManager::class);
56-
Livewire::component('teams.delete-team-form', DeleteTeamForm::class);
57-
}
58-
}
59-
});
6038
}
6139

6240
/**
@@ -91,6 +69,26 @@ public function boot()
9169
if (config('jetstream.stack') === 'inertia' && class_exists(Inertia::class)) {
9270
$this->bootInertia();
9371
}
72+
73+
if (config('jetstream.stack') === 'livewire' && class_exists(Livewire::class)) {
74+
Livewire::component('navigation-menu', NavigationMenu::class);
75+
Livewire::component('profile.update-profile-information-form', UpdateProfileInformationForm::class);
76+
Livewire::component('profile.update-password-form', UpdatePasswordForm::class);
77+
Livewire::component('profile.two-factor-authentication-form', TwoFactorAuthenticationForm::class);
78+
Livewire::component('profile.logout-other-browser-sessions-form', LogoutOtherBrowserSessionsForm::class);
79+
Livewire::component('profile.delete-user-form', DeleteUserForm::class);
80+
81+
if (Features::hasApiFeatures()) {
82+
Livewire::component('api.api-token-manager', ApiTokenManager::class);
83+
}
84+
85+
if (Features::hasTeamFeatures()) {
86+
Livewire::component('teams.create-team-form', CreateTeamForm::class);
87+
Livewire::component('teams.update-team-name-form', UpdateTeamNameForm::class);
88+
Livewire::component('teams.team-member-manager', TeamMemberManager::class);
89+
Livewire::component('teams.delete-team-form', DeleteTeamForm::class);
90+
}
91+
}
9492
}
9593

9694
/**

0 commit comments

Comments
 (0)