Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

DNABeast's avatar

Livewire 3 and loading AlpineJS multiple times.

I'm trying out Livewire3. It's pretty neat that it automatically pulls in when a component it used and that it includes alpinejs.

But I've run into a problem. What if I have a two page site. It uses a tiny bit of alpine in the navigation. The second page has a Livewire component so alpine is automatically loaded. But the first page doesn't have access to alpine at all. So I load AlpineJS in the layout. But then AlpineJS is being loaded on the second page twice. (This creates unexpected results)

Possible solutions...

Turn off auto assets in the config

Manually load assets in the layout so that AlpineJS and Livewire are always available even when Livewire is not required.

But then I'm loading Livewire when it's not required.

Manually load Livewire on Livewire pages

If Livewire isn't loaded, load AlpineJS by itself

This is probably what I'll have to do. Blade has helpers that will do this fairly nicely. It does suck to have to download AlpineJS multiple times over the course of browsing to different pages.

remove AlpineJS from the autoloaded Livewire functionality

Then AlpineJS loads each time from cache and when Livewire is required AlpineJs is already there waiting.

I struggled to get the @livewireScriptConfig to work properly. It seems to result in an error. (Possibly something that needs massaging during beta.) I probably won't waste more time on this despite it potentially being the best solution in the long run.

It just seems odd that this would be a very common situation but with no simple solution.

If you're reading this and think you've spotted a better solution please do reply.

0 likes
2 replies
LaryAI's avatar
Level 58

One possible solution to this problem is to manually load AlpineJS and Livewire in the layout, but only when they are required. This can be achieved by using conditional statements in your layout file.

Here's an example of how you can implement this solution in your layout file:

<!DOCTYPE html>
<html>
<head>
    <!-- Include your other head elements here -->
    
    @if (isset($livewire))
        <!-- Load Livewire -->
        <script src="{{ asset('livewire.js') }}"></script>
    @endif
    
    @if (isset($alpine))
        <!-- Load AlpineJS -->
        <script src="{{ asset('alpine.js') }}"></script>
    @endif
</head>
<body>
    <!-- Your page content here -->
    
    @yield('content')
</body>
</html>

In your Livewire component, you can then set the $livewire and $alpine variables in the mount method based on whether Livewire or AlpineJS is required for that specific component.

<?php

namespace App\Http\Livewire;

use Livewire\Component;

class MyComponent extends Component
{
    public $livewire = true;
    public $alpine = true;

    public function mount()
    {
        // Set $livewire and $alpine based on your logic
        // For example, if Livewire is not required for this component, you can set $livewire to false
        // If AlpineJS is not required for this component, you can set $alpine to false
    }

    public function render()
    {
        return view('livewire.my-component');
    }
}

By using this approach, you can control when Livewire and AlpineJS are loaded in your layout, ensuring that they are only loaded when necessary and avoiding duplicate loading on pages where they are not required.

Please or to participate in this conversation.