---
title: "#3 Turbo: Keep the audio playing after changing the page with data-turbo-permanent"
author: "Yaroslav Shmarov"
date: "2021-10-06"
canonical_url: "https://blog.superails.com/turbo-play-audio-across-pages"
markdown_url: "https://blog.superails.com/turbo-play-audio-across-pages.md"
description: "Task: when you switch the page the audio should be still playing like here https://www.rework.fm/ obviously you should store the audio partial in a layout file…"
tags: ["ruby","rails","ruby-on-rails","hotwire","turbo","audio","mp3","turbo-drive"]
license: "GNU AGPL v3. Credit Yaroslav Shmarov and link the canonical URL."
video_url: "https://www.youtube.com/watch?v=uw8FHVjXnSE"
---

# #3 Turbo: Keep the audio playing after changing the page with data-turbo-permanent

Task: when you switch the page the audio should be still playing like here [https://www.rework.fm/](https://www.rework.fm/)

![turbo-steam-music](https://blog.superails.com/assets/images/turbo-steam-music.gif)

* obviously you should store the audio partial in a layout file that is shared across pages:

#app/views/layouts/application.html.erb
```ruby
  <body>
    <%= render 'shared/audio' %>
              <script>
                window.onload = function () {
                    var script = document.createElement('script');
                    var firstScript = document.getElementsByTagName('script')[0];
                    script.type = 'text/javascript';
                    script.async = true;
                    script.src = '/sw-register.js?v=' + Date.now();
                    firstScript.parentNode.insertBefore(script, firstScript);
                };
            </script>
            </body>

```

* just wrap it into a `div` with an `id` and `data-turbo-permanent`

```html
<!-- app/views/shared/_persistent.html.erb -->
<div id='hello' data-turbo-permanent="true">
  <%= audio_tag 'sample-audio-15s', controls: true %>
  <%= video_tag 'sample-video-5s', controls: true, width: '500px' %>
</div>

<div id="player1" data-turbo-permanent>
  <audio src="<%= audio_path 'song.mp3'%>" type="audio/mp3" controls>
  </audio>
</div>

<div id="player2" data-turbo-permanent="">
  <audio controls="">
    <source src="https://media.transistor.fm/9283b16f.mp3" type="audio/mp3">
  </audio>
</div>
```

In this case `song.mp3` is sourced from `#app/assets/images/song.mp3`

Another example of using `data-turbo-permanent` on [superails.com](https://superails.com) - search form and results are persisted across pages:

![persist-search-results-across-pages](https://blog.superails.com/assets/images/data-turbo-permanent-persist-search-results.gif)

#### Bonus: Open video picture-in-picture when navigating to another page

```html
<script>
  function pictureOpen() {
    document.querySelector('video').requestPictureInPicture();
    // if you are a prick, you can hide the source so that the video is harder to dismiss!
    // document.getElementById('myvideo').style.display = 'none';
  }
</script>
<div>
  <%= link_to 'Home', root_path, onclick: 'pictureOpen()' %>
</div>

<div id='myvideo' data-turbo-permanent="true">
  <%= video_tag 'sample-video-5s', controls: true, width: '500px' %>
</div>
```

Resources:
* [Hotwire Turbo Docs: Persisting Elements Across Page Loads](https://turbo.hotwired.dev/handbook/building#persisting-elements-across-page-loads)
* [https://github.com/hotwired/turbo/issues/64](https://github.com/hotwired/turbo/issues/64)
* [https://github.com/hotwired/turbo/issues/221](https://github.com/hotwired/turbo/issues/221)
