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))
}
}
When you redirect after the form submission, there is no with the same id, so you get frame missing error. For some details and my first attempt at a reusable solution:
https://stackoverflow.com/a/75704489/207090
I don't want to post a duplicate answer, but I'll mention that there is a turbo:frame-missing event, that you can listen for and handle the error:
One caveat with doing a simple visit(response.url) is that it will be the second request to that url (first one is from the redirect).
The ideal place to handle this would be at the redirect response, because that's where you'd know if form submission was successful or not and send redirect as a regular Turbo.visit outside of the frame. Which you can do by sending some javascript in a turbo_stream:
Make a custom turbo_stream.redirect action
Tag for "redirect" action would look like this:
Add new action to Turbo to handle it on the front end:
https://turbo.hotwired.dev/handbook/streams#custom-actions
Use generic action method to render it in a controller or in a template:
You can also add redirect method to turbo_stream helper:
https://stackoverflow.com/questions/75738570/getting-a-turbo-frame-error-of-content-missing