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

changing turbo frame src manually doesn't update main url #468

Closed
alexgerber opened this issue Nov 23, 2021 · 1 comment · Fixed by #1135
Closed

changing turbo frame src manually doesn't update main url #468

alexgerber opened this issue Nov 23, 2021 · 1 comment · Fixed by #1135

Comments

@alexgerber
Copy link

alexgerber commented Nov 23, 2021

Hi, is this a bug or something intentionally left out? On latest turbo release.

When using a turbo frame with the new data-turbo-action="advance" directive ie:

<turbo-frame data-turbo-action="advance" data-search-target="frame">
  <a href="/articles?page=2" rel="next">Next page</a>
</turbo-frame>

When manually selecting the turbo frame and changing the src in stimulus controller
ie frameTarget.src = '/' it only changes the src of the frame, not url.

It would be nice to be able to change the src programmatically and have it sync with the main url as well, like normal link clicks. Thanks, let me know what you think, or if I missed something.

@alexgerber alexgerber changed the title changing turbo frame src manually doesn't update url changing turbo frame src manually doesn't update main url Nov 23, 2021
seanpdoyle added a commit to seanpdoyle/turbo that referenced this issue Nov 24, 2021
@seanpdoyle
Copy link
Contributor

seanpdoyle commented Nov 24, 2021

This is a current limitation that is likely to stick around in 7.1.0.

In cases where you need to promote a Frame Visit to a Page Visit programmatically (like in a Stimulus Controller), you could synthesize an <a> element, declare the desired frameTarget.src as the <a href="...">, set [data-turbo-action] on the element, then click it.

function promoteToFrameVisit(element /*: FrameElement */, src /*: string */, action = "advance") {
  const link = document.createElement("a")
  link.setAttribute("href", src)
  link.setAttribute("data-turbo-action", action) // you can skip this is the <turbo-frame> already declares [data-turbo-action]
  link.setAttribute("hidden", "")
  element.appendChild(link)
  link.click()
  element.removeChild(link)
}

promoteToFrameVisit(this.frameTarget, "/", "replace")

Alternatively, the <turbo-frame> could have a descendant <a hidden> element that the controller clicks.

To share a little extra context, the current internal structure of the Turbo Frames portion of the codebase makes it challenging to retain contextual information throughout a Frame navigation.

For example, once an <a> element is clicked, a <form> element is submitted (often times with <button> or <input type="submit"> submitter), or a <turbo-frame> element's [src] attribute is changed programmatically, the methods that respond to that action cannot access the original elements' [data-*] attributes. This is a problem when trying to infer additional information. All that's available is the value of the [src].

There are efforts in-progress to refactor and restructure the Turbo internals , but they're unlikely to ship until a stable 7.1.0 has been released.

seanpdoyle added a commit to seanpdoyle/turbo that referenced this issue Jan 22, 2024
Closes [hotwired#489][]
Closes [hotwired#468][]

First, add test coverage to exercise the `<turbo-frame id="frame">` and
`<turbo-frame id="hello" target="frame">` as outlined in a
[comment on hotwired#489][].

Next, add coverage to support driving a `<turbo-frame
data-turbo-action="advance">` through the `Turbo.visit` method. To pass
that test coverage, invoke
`frameElement.delegate.proposeVisitIfNavigatedWithAction(frameElement)`
prior to modifying the element's `[src]` attribute.

To support the implementation changes necessary to pass the tests, this
commit makes one change to the public interface, and another to an
internal interface.

* `FrameElement.getElementById(id: string)` - unify the process to
  internally resolve a `<turbo-frame>` element by its `[id]`. Update
  call sites to use this new method

* `FrameController.proposeVisitIfNavigatedWithAction(frame, action)` -
  flatten the variable arguments into a single `action` argument. Move
  the `getVisitAction` call to the call sites that require it, and pass in
  the `action` directly when its available

[hotwired#489]: hotwired#489
[hotwired#468]: hotwired#468
[comment on hotwired#489]: hotwired#489 (comment)
seanpdoyle added a commit to seanpdoyle/turbo that referenced this issue Jan 22, 2024
Closes [hotwired#489][]
Closes [hotwired#468][]

First, add test coverage to exercise the `<turbo-frame id="frame">` and
`<turbo-frame id="hello" target="frame">` as outlined in a
[comment on hotwired#489][].

Next, add coverage to support driving a `<turbo-frame
data-turbo-action="advance">` through the `Turbo.visit` method. To pass
that test coverage, invoke
`frameElement.delegate.proposeVisitIfNavigatedWithAction(frameElement)`
prior to modifying the element's `[src]` attribute.

To support the implementation changes necessary to pass the tests, this
commit changes the `proposeVisitIfNavigatedWithAction(frame, action)`
interface by flattening the variable arguments into a single `action`
argument, then moving the `getVisitAction` call to the call sites that
require it. The call sites that know their `action` pass it in directly
when its available.

[hotwired#489]: hotwired#489
[hotwired#468]: hotwired#468
[comment on hotwired#489]: hotwired#489 (comment)
seanpdoyle added a commit to seanpdoyle/turbo that referenced this issue Jan 30, 2024
Closes [hotwired#489][]
Closes [hotwired#468][]

First, add test coverage to exercise the `<turbo-frame id="frame">` and
`<turbo-frame id="hello" target="frame">` as outlined in a
[comment on hotwired#489][].

Next, add coverage to support driving a `<turbo-frame
data-turbo-action="advance">` through the `Turbo.visit` method. To pass
that test coverage, invoke
`frameElement.delegate.proposeVisitIfNavigatedWithAction(frameElement)`
prior to modifying the element's `[src]` attribute.

To support the implementation changes necessary to pass the tests, this
commit changes the `proposeVisitIfNavigatedWithAction(frame, action)`
interface by flattening the variable arguments into a single `action`
argument, then moving the `getVisitAction` call to the call sites that
require it. The call sites that know their `action` pass it in directly
when its available.

[hotwired#489]: hotwired#489
[hotwired#468]: hotwired#468
[comment on hotwired#489]: hotwired#489 (comment)
afcapel pushed a commit that referenced this issue Feb 2, 2024
Closes [#489][]
Closes [#468][]

First, add test coverage to exercise the `<turbo-frame id="frame">` and
`<turbo-frame id="hello" target="frame">` as outlined in a
[comment on #489][].

Next, add coverage to support driving a `<turbo-frame
data-turbo-action="advance">` through the `Turbo.visit` method. To pass
that test coverage, invoke
`frameElement.delegate.proposeVisitIfNavigatedWithAction(frameElement)`
prior to modifying the element's `[src]` attribute.

To support the implementation changes necessary to pass the tests, this
commit changes the `proposeVisitIfNavigatedWithAction(frame, action)`
interface by flattening the variable arguments into a single `action`
argument, then moving the `getVisitAction` call to the call sites that
require it. The call sites that know their `action` pass it in directly
when its available.

[#489]: #489
[#468]: #468
[comment on #489]: #489 (comment)
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.

2 participants