From 9c1048f609c85b928149f4dcca45c2f9ac4e6d23 Mon Sep 17 00:00:00 2001 From: Alan Grainger <16197738+alangrainger@users.noreply.github.com> Date: Fri, 12 Jun 2026 12:18:52 +1200 Subject: [PATCH] Guard deferred video autoplay against slide changes --- app/src/client/lightbox.ts | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/app/src/client/lightbox.ts b/app/src/client/lightbox.ts index a0c014c..4451983 100644 --- a/app/src/client/lightbox.ts +++ b/app/src/client/lightbox.ts @@ -167,21 +167,30 @@ export function initLightbox () { // fires contentActivate/contentDeactivate as slides become / stop being the // current slide, including the initial slide on open. if (state.lightboxConfig.autoPlayVideos) { - state.lightbox.on('contentActivate', ({ content }: { content: { element?: HTMLElement } }) => { + state.lightbox.on('contentActivate', ({ content }: { content: { element?: HTMLElement, slide?: { isActive: boolean } } }) => { const video = content.element?.querySelector('video') - if (video) { - setTimeout(() => { - video.play().catch((err: Error) => { - // Only mute and retry if the browser explicitly blocked unmuted autoplay. - if (err.name === 'NotAllowedError') { - video.muted = true - video.play().catch(() => {}) - } else if (err.name === 'AbortError') { - video.play().catch(() => {}) - } - }) - }, 50) - } + if (!video) return + // Brief delay so PhotoSwipe finishes attaching the video to the DOM + // (deferred until the opening animation completes) - calling play() + // synchronously gets interrupted by the attach and rejects with an + // AbortError. + setTimeout(() => { + if (!content.slide?.isActive) return + video.play().catch((err: Error) => { + if (err.name === 'NotAllowedError') { + // The browser blocked unmuted autoplay without a user gesture + // (e.g. a deep link straight to a video slide) - retry muted. + video.muted = true + video.play().catch(() => {}) + } else if (err.name === 'AbortError' && content.slide?.isActive) { + // A pending play() was interrupted, e.g. by our contentDeactivate + // pause rejecting it while the video was still buffering. Only + // retry if the slide is still current, so we never resume audio + // behind a different slide. + video.play().catch(() => {}) + } + }) + }, 50) }) }