I am using Laravel Livewire for navigation within my application. My default layout includes the following scripts:
<div class="flex grow">
<x-layouts.sidebar />
<!-- End of Sidebar -->
<!-- Wrapper -->
<div class="wrapper flex grow flex-col">
<!-- Header -->
<x-layouts.header />
<main class="grow content pt-5" id="content" role="content">
{{ $slot }}
</main>
<x-layouts.footer />
<!-- End of Footer -->
</div>
<!-- End of Wrapper -->
</div>
<script src="v2/assets/js/core.bundle.js"></script>
<script src="v2/assets/vendors/apexcharts/apexcharts.min.js"></script>
<script src="v2/assets/js/widgets/general.js"></script>
<script src="v2/assets/js/layouts/demo1.js"></script>
When I fully reload the page, everything works fine—JavaScript functions like toggling the sidebar, dropdowns, and other interactions behave as expected. However, when I navigate between components using Livewire.navigate, the JavaScript stops working. Even navigating to the same page using Livewire doesn’t restore the functionality. Only a full page refresh fixes the issue.
What I Tried:
Livewire Event Listeners:
I attempted to reinitialize the scripts using livewire:navigated, livewire:navigating, and livewire:navigate events like so:
<script>
document.addEventListener('livewire:navigated', () => {
console.log('true working');
@php
$scripts = [
public_path('v2/assets/js/core.bundle.js'),
public_path('v2/assets/vendors/apexcharts/apexcharts.min.js'),
public_path('v2/assets/js/widgets/general.js'),
public_path('v2/assets/js/layouts/demo1.js'),
];
foreach ($scripts as $script) {
if (file_exists($script)) {
echo file_get_contents($script);
} else {
echo "// File not found: " . basename($script) . "n";
}
}
@endphp
});
</script>
The event listener triggers and the scripts are loaded in the DOM (verified via the Network tab), but the functionality doesn’t work after navigation.
Manual Script Addition:
I manually included a custom script file outside the webpackUniversalModuleDefinition function, and it worked fine even after Livewire navigation. This led me to suspect the issue might be related to the syntax or how Webpack wraps the bundled scripts.
Suspicions and Observations:
Webpack Universal Module Definition:
The scripts are wrapped using the following pattern:
(function webpackUniversalModuleDefinition(root, factory) {
if (typeof exports === 'object' && typeof module === 'object')
module.exports = factory();
else if (typeof define === 'function' && define.amd)
define([], factory);
else {
var a = factory();
for (var i in a) (typeof exports === 'object' ? exports : root)[i] = a[i];
}
})(self, function() {
// Module code here
});
I suspect this wrapping might be why the functionality doesn’t work after Livewire navigation, but I’m unsure why it works on a full reload.
Global Scope Issue:
Perhaps the bundled scripts are not exposing their functionality to the global scope (window or self), and thus are inaccessible after Livewire partial updates.
Script Execution on Navigation:
I noticed that if I add logic outside the Webpack wrapper (e.g., directly in the tag or at the bottom of the script file), it works fine. This leads me to think the issue lies in how the functions are registered or initialized.
- Why do the Webpack-wrapped scripts work on a full reload but not after Livewire navigation?
- Is the issue related to how Webpack exposes functions or registers them to the global scope?
- What is the best way to ensure that such scripts work seamlessly after Livewire navigation?
- Could this issue be related to caching, module initialization, or Livewire’s partial DOM updates?
I want the JavaScript functionality (e.g., sidebar toggles, dropdowns, etc.) to work after navigating to a new component using Livewire.navigate, just as it works after a full page reload.
2
Answers
see this link: https://livewire.laravel.com/docs/navigate#dont-rely-on-domcontentloaded
you should use
livewire:navigated
instead ofDOMContentLoaded
the
DOMContentLoaded
only triggered once after page hard refresh. but when you navigate between pages usingwire:navigate
, theDOMContentLoaded
is not triggering anymore, instead you can uselivewire:navigated
event. it’s triggered in both page hard refresh (likeDOMContentLoaded
) and after navigating between pagesI don’t know whether it is right for your answer but try to add you script inside
@script
and@endscript
. This ensured my scripts are worked even after Livewire navigation.Reference -> https://laracasts.com/discuss/channels/livewire/script-not-executing-in-livewire-component-view