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

maharzan's avatar

Custom JS not executing on rerenders with Livewire

I am trying to make Photoswipe JS work on images that are loaded in Laravel Livewire. The JS file is include in app.blade.php

 @vite(['resources/js/lightbox.js'])

The JS executes well when I first load and click the image, a lightbox appears. But once I go to other pages and come back, it no longer works and I have to refresh. I wonder what am I missing.. I have tried clearing cache and views and also saw the wire:key=rand() method in the div which have failed.. I also read about emit method but not sure how to do it in the custom js.

The lightbox.js has this code (from documentation of Photoswipe)

import PhotoSwipeLightbox from 'photoswipe/lightbox';
import 'photoswipe/style.css';

const lightbox = new PhotoSwipeLightbox({
  gallery: '#mygallery',
  children: 'a',
  initialZoomLevel: 'fit',
  secondaryZoomLevel: 'fit',
  pswpModule: () => import('photoswipe')
});
lightbox.init();

Please advise.

0 likes
1 reply
maharzan's avatar

Ah, I did this in lightbox.js and it seems to be working.. not sure if its the best solution. If you still have any solutions, please leave them here.

Changed

lightbox.init();

to

document.addEventListener('livewire:navigated', () => {
   lightbox.init();
});

Please or to participate in this conversation.