Skip to content

Commit

Permalink
Fix Livewire component premature registration (#1390)
Browse files Browse the repository at this point in the history
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 () => '');`
  • Loading branch information
Omranic authored Oct 18, 2023
1 parent 5b445d2 commit 649364c
Showing 1 changed file with 20 additions and 22 deletions.
42 changes: 20 additions & 22 deletions src/JetstreamServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,6 @@ class JetstreamServiceProvider extends ServiceProvider
public function register()
{
$this->mergeConfigFrom(__DIR__.'/../config/jetstream.php', 'jetstream');

$this->callAfterResolving(BladeCompiler::class, function () {
if (config('jetstream.stack') === 'livewire' && class_exists(Livewire::class)) {
Livewire::component('navigation-menu', NavigationMenu::class);
Livewire::component('profile.update-profile-information-form', UpdateProfileInformationForm::class);
Livewire::component('profile.update-password-form', UpdatePasswordForm::class);
Livewire::component('profile.two-factor-authentication-form', TwoFactorAuthenticationForm::class);
Livewire::component('profile.logout-other-browser-sessions-form', LogoutOtherBrowserSessionsForm::class);
Livewire::component('profile.delete-user-form', DeleteUserForm::class);

if (Features::hasApiFeatures()) {
Livewire::component('api.api-token-manager', ApiTokenManager::class);
}

if (Features::hasTeamFeatures()) {
Livewire::component('teams.create-team-form', CreateTeamForm::class);
Livewire::component('teams.update-team-name-form', UpdateTeamNameForm::class);
Livewire::component('teams.team-member-manager', TeamMemberManager::class);
Livewire::component('teams.delete-team-form', DeleteTeamForm::class);
}
}
});
}

/**
Expand Down Expand Up @@ -91,6 +69,26 @@ public function boot()
if (config('jetstream.stack') === 'inertia' && class_exists(Inertia::class)) {
$this->bootInertia();
}

if (config('jetstream.stack') === 'livewire' && class_exists(Livewire::class)) {
Livewire::component('navigation-menu', NavigationMenu::class);
Livewire::component('profile.update-profile-information-form', UpdateProfileInformationForm::class);
Livewire::component('profile.update-password-form', UpdatePasswordForm::class);
Livewire::component('profile.two-factor-authentication-form', TwoFactorAuthenticationForm::class);
Livewire::component('profile.logout-other-browser-sessions-form', LogoutOtherBrowserSessionsForm::class);
Livewire::component('profile.delete-user-form', DeleteUserForm::class);

if (Features::hasApiFeatures()) {
Livewire::component('api.api-token-manager', ApiTokenManager::class);
}

if (Features::hasTeamFeatures()) {
Livewire::component('teams.create-team-form', CreateTeamForm::class);
Livewire::component('teams.update-team-name-form', UpdateTeamNameForm::class);
Livewire::component('teams.team-member-manager', TeamMemberManager::class);
Livewire::component('teams.delete-team-form', DeleteTeamForm::class);
}
}
}

/**
Expand Down

0 comments on commit 649364c

Please sign in to comment.