Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Videos with autoplay keep playing after a page change #1017

Open
n-studio opened this issue Oct 2, 2023 · 6 comments
Open

Videos with autoplay keep playing after a page change #1017

n-studio opened this issue Oct 2, 2023 · 6 comments

Comments

@n-studio
Copy link

n-studio commented Oct 2, 2023

If I play a video with the autoplay attribute, and then go to another page using Turbo, the video (that is supposed to be removed from the DOM) plays again. The video is not visible but the audio still plays.

If I disable Turbo, the video will stop when changing to a new page as expected.

Same if I remove the autoplay attribute, it won't replay the video when changing to a new page.

<video controls="controls" autoplay="autoplay" loop="loop" src="/video.mp4"></video>
<a href="/page2" data-turbo="true">Then go to page 2</a>

Here is a repository reproducing the issue: https://github.com/n-studio/turboVideoBug

It will occur on Safari, Firefox and Chrome on macOS Sonoma.

@domchristie
Copy link
Contributor

You may find the alternative solution here useful: turbolinks/turbolinks#432 (Event names need updating to turbo rather than turbolinks)

@n-studio
Copy link
Author

n-studio commented Oct 4, 2023

Thank you! 🙏

@n-studio
Copy link
Author

n-studio commented Oct 4, 2023

Is there any plan to port your PR to hotwired/turbo?

@seanpdoyle
Copy link
Contributor

Is there a possibility that some of the work done in #1019 might improve this experience?

@domchristie
Copy link
Contributor

domchristie commented Oct 12, 2023

Is there any plan to port your PR to hotwired/turbo?

@n-studio no plans yet, sorry (I have tended to avoid autoplay because it's not reliable in Safari due to their autoplay policy.)

Is there a possibility that some of the work done in #1019 might improve this experience?

@seanpdoyle Possibly, although from the sounds of things, #1019 only affects page refreshes. The problem here is that autoplay elements get cloned to the cache, and start playing behind-the-scenes. I've not gone through the PR fully, but it looks like the cloning behaviour still exists.

@scuml
Copy link
Contributor

scuml commented Mar 14, 2024

This is still occurring in Turbo 7 and 8. This is an updated version of @domchristie 's workaround by until I can start on a PR.

/* Store autoplay elements so Turbo does not play them while updating the snapshot */
let autoplayIds = []
document.addEventListener('turbo:before-cache', () => {
  const autoplayElements = document.querySelectorAll('[autoplay]')
  autoplayElements.forEach((element) => {
    if (!element.id) throw 'Turbo requires autoplay elements to have an ID attribute'
    autoplayIds.push(element.id)
    element.removeAttribute('autoplay')
  })
})

document.addEventListener('turbo:before-render', (event) => {
  autoplayIds = autoplayIds.reduce((ids, id) => {
    const autoplay = event.detail.newBody.querySelector(`#${id}`)
    if (autoplay) autoplay.setAttribute('autoplay', true)
    else ids.push(id)
    return ids
  }, [])
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants