Hello there, hoping you’re having a good week/end.
So, i had an issue in that i had resources that would need to be accessed with .turbo_stream formats. But, i needed them not to update the browser url. The standard Turbo way(that i know of) is that you would place it within a frame, then it would update the frame’s url and not the browsers.
But, i had cases where the link was not inside a frame tag. It was a normal link ( <a> tag), i don’t want to rely on Rails-UJS to do that through supplying data-method attribute on the frame. In case it’s removed in the future(from our App).
For this reason, I created a simple Stimulus controller, that would use Request.js to send a GET request to the server(without updating the browser history)
import { Controller } from "@hotwired/stimulus"
import { get } from "@rails/request.js"
export default class extends Controller {
requestTurboStream({ currentTarget }) {
get(currentTarget.dataset.url, {
responseKind: "turbo-stream",
})
}
}
Then, i would tranform the linkinto something like
This is pretty much exactly what I do, haven’t had a general need yet but I use this in a number of controllers that have a specific need to be handled like you suggest.
The understanding i have of Turbo Frames is that if i’m going to replace a single element on the page, i would use frames. If more than one, i use Turbo Streams. Am i wrong?.
Another way to put this is, turbo-frames CAN only update a single page of the page. turbo-streams CAN be used to update more than one part of the page.
I’m not familiar with any suggestions around this being a discouraged pattern. The documentation neither suggests, nor discourages combining them for GET requests.