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: Updating a single element
In this example, Turbo.visit makes a request to the server for the /my-page URL and replaces the contents of the element with the id my-element with the response.
Example 2: Replacing an entire page
In this example, Turbo.visit makes a request to the server for the /new-page URL and replaces the entire current page with the response.
Example 3: Appending a new element
In this example, Turbo.visit makes a request to the server for the /new-element URL and appends the response as a new element inside the element with the id my-container.
Example 4: Updating a form
In this example, Turbo.visit makes a request to the server for the /new-form-data URL and updates the form with the response.
Example 5: Modifying the browser history
In this example, Turbo.visit makes a request to the server for the /new-page URL and replaces the current history entry with the response. This allows the user to use the back button to navigate back to the previous page.