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

Armghan's avatar

Does the Livewire 3 @script directive even work?

Does the Livewire 3 @script @endscript directive even work?

I did it based on the documentation: https://livewire.laravel.com/docs/events#listening-for-events-inside-component-scripts

Yet it doesn't trigger a custom function of mine with:

@script
<script>
    $wire.on('post-created', () => {
                        playSound("{!! asset('/source/message-beep.mp3') !!}");
    });
</script>
@endscript

Infact it actually echoes the @script @endscript as words.

Am I doing something wrong? Like am I placing them in the Component Blade while they should be placed outside the component (Doesn't make sense but still asking)? Please help me... I'm making a chat widget...

(Note: I have already used:

                <script>
                    document.addEventListener('livewire:init', () => {
                       Livewire.on('visitor-message-created', () => {
                        playSound("{!! asset('/source/message-beep.mp3') !!}");
                       });
                    });
                </script>

which is working find when I don't lazy load.

Now when I lazy load the component like below, while trying the @script directive like the docs:

                		<livewire:visitors 
                		:currentPath="request()->url()"
						lazy
                		/>

Atleast it doesn't show the @script @endscript as echoed text but obviously still doesn't perform it's event recieving action)

0 likes
5 replies
Armghan's avatar
Armghan
OP
Best Answer
Level 1

Anyone that comes across this. Apparently the issue itself was with livewire. (Mine was 3.0.1 and it needed to be 3.3)

I just updated it: composer update livewire/livewire

Working perfectly now

2 likes
kosta006's avatar

same I have never got the @script tags to work at all. Doesn't matter what version even v3.5 on a new install does nothing with the script directive

1 like
Armghan's avatar

@kosta006 Unfortunately to my experience, livewire in itself isn't completely dependable. I tend to do minor stuff with it and just use basic jquery or alpine for as much other stuff possible.

Please or to participate in this conversation.