diff --git a/src/Events/LivewireDispatcher.php b/src/Events/LivewireDispatcher.php index fd3bea4..d677727 100644 --- a/src/Events/LivewireDispatcher.php +++ b/src/Events/LivewireDispatcher.php @@ -16,11 +16,11 @@ public function register(): void } /** Injects assets inside every full-page response */ - public function handle(RequestHandled $handled) + public function handle(RequestHandled $handled): void { $identifier = 'NativePHP Livewire Dispatcher'; $html = $handled->response->getContent(); - $originalContent = $handled->response->original; + $originalContent = $handled->response->original ?? null; if (! $handled->response->isSuccessful()) { return; @@ -50,7 +50,13 @@ public function handle(RequestHandled $handled) HTML) ); - $handled->response->original = $originalContent; + // Laravel dispatches the ResponseHandled event even for response + // objects that don't include the `original` property. + // The typehint in Laravel core is wrong, so we ignore + /* @phpstan-ignore function.alreadyNarrowedType */ + if (property_exists($handled->response, 'original')) { + $handled->response->original = $originalContent; + } } /** Injects assets into given html string (taken from Livewire's injection mechanism) */