fetch(href, {
headers: {
Accept: "text/vnd.turbo-stream.html",
},
})
.then(r => r.text())
.then(html => Turbo.renderStreamMessage(html))
// optionally reflect the url .then(_ => history.replaceState(history.state, "", href))
import { Controller } from '@hotwired/stimulus'
export default class extends Controller {
static values = {
src: String,
}
connect() {
fetch(this.srcValue, {
headers: {
Accept: "text/vnd.turbo-stream.html",
},
}).then(r => r.text())
.then(html => Turbo.renderStreamMessage(html))
}
}
Trying to use Turbo.visit, but within a Turbo Frame
For reasons, I needed to be able to programmatically navigate within a Turbo Frame. I was hoping that Turbo.visit(path) would work, but it always affected the entire page. I found a feature that is only lightly documented. If I can select the turbo-frame element I can set the "src" attribute to make it load a new path. Here is a Stimulus controller I made to make table rows clickable. It will work within a Turbo Frame or just a regular table on a page: