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))
}
}
Example 1: Using anchor with Turbo.visit
In this example, Turbo.visit makes a request to the server for the /my-page URL with the anchor #my-anchor, and replaces the current page with the response. The scroll option is set to true, which scrolls the page to the #my-anchor element when the response is received.
Example 2: Using turbo_frame with Turbo.visit
In this example, Turbo.visit makes a request to the server for the /my-page URL and replaces the contents of the turbo_frame with the id my-frame with the response. Note that the target option is used to specify the turbo_frame to update.
Example 3: Using turbo_frame with append action
In this example, Turbo.visit makes a request to the server for the /my-page URL and appends the response as a new element inside the turbo_frame with the id my-frame. Note that the action option is set to append to specify that the response should be appended to the current contents of the turbo_frame.
Example 4: Using turbo_frame with a form
In this example, Turbo.visit makes a request to the server with the data from the form with the id my-form. The action option is set to replace to specify that the response should replace the current contents of the turbo_frame with the id my-frame. The body option is used to send the form data with the request.