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

Turbo Stream operations ignore [data-turbo-permanent] #623

Closed
seanpdoyle opened this issue Jul 6, 2022 · 2 comments · Fixed by #688
Closed

Turbo Stream operations ignore [data-turbo-permanent] #623

seanpdoyle opened this issue Jul 6, 2022 · 2 comments · Fixed by #688

Comments

@seanpdoyle
Copy link
Contributor

Related to #622 (comment).

Unlike the PageRenderer and FrameRenderer implementations, the StreamActions do not preserve permanent elements

You can experiment with the code on JSFiddle:

<html>                                        
  <head>                                  
    <script type="importmap">                                                                                      
      {
        "imports": {
          "@hotwired/turbo": "https://cdn.skypack.dev/@hotwired/turbo"
        }
      }
    </script>
    <script type="module">
      import "@hotwired/turbo"
      
      addEventListener("click", ({ target }) => {
        if (target.matches("button")) {
          const { content } = target.querySelector("template")
          target.append(content.cloneNode(true))
        }
      })
    </script>
    <script type="module" src="https://unpkg.com/es-module-shims@0.10.1/dist/es-module-shims.js"></script>
  </head>
  <body>
    <p id="permanent" data-turbo-permanent>This should not change when you click the button</p>
      
    <button type="button">
      Click me, and hopefully nothing changes

      <template>
        <turbo-stream action="replace" target="permanent">
          <template><p id="permanent" data-turbo-permanent>Uh oh, this changed when you clicked the button!</p></template>
        </turbo-stream>
      </template>
    </button>
  </body>
</html>

Is this intended behavior? Permanent elements are a Turbolinks concept, and pre-date Frames and Streams.

Personally, I'm not sure what I'd expect to be "correct" behavior.

On one side of the argument, "permanence" is meaningful during navigation, but might not fit the idea of a Stream operation. On the opposing side, "permanence" could implies that it should be sturdy enough to be immutable in the face of page updates.

@dhh
Copy link
Member

dhh commented Jul 6, 2022

I think it makes sense for permanent elements to survive stream actions. If you DON'T want a permanent element to survive, you can always just not mark it permanent in the stream content.

seanpdoyle added a commit to seanpdoyle/turbo that referenced this issue Aug 17, 2022
Closes hotwired#623

Refactor the `Snapshot` implementation to make the permanent element
finding code re-usable outside that module.

Then, introduce the `StreamMessageRenderer` class, and re-use that code.

The `StreamMessageRenderer` class also implements `BardoDelegate`, and
relies on `Bardo.preservingPermanentElements` to manage elements across
their `<turbo-stream>` rendering lifespan.
seanpdoyle added a commit to seanpdoyle/turbo that referenced this issue Aug 17, 2022
Closes hotwired#623

Refactor the `Snapshot` implementation to make the permanent element
finding code re-usable outside that module.

Then, introduce the `StreamMessageRenderer` class, and re-use that code.

The `StreamMessageRenderer` class also implements `BardoDelegate`, and
relies on `Bardo.preservingPermanentElements` to manage elements across
their `<turbo-stream>` rendering lifespan.
@dhh dhh closed this as completed in cf3d726 Sep 13, 2022
@blump
Copy link

blump commented Feb 20, 2023

Is it on the roadmap for data-turbo-permanent to work with StreamActions?
I tried the morphdom library to work around it but it causes me problems with my stimulus controllers ...

I'd like to be able to implement this on an input file, to avoid having to re-upload the file after a 422 response.

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

Successfully merging a pull request may close this issue.

3 participants