Frames
A Turbo Frame is a part of a page that loads and navigates by itself. Links and forms inside it replace only the frame.
The example's stats panel loads once it scrolls into view (routes/index.tsx):
<turbo-frame id="stats" src="/stats" loading="lazy">
<span class="loading loading-dots"></span>
</turbo-frame>
/stats is an ordinary page. When a frame asks, the request carries the header
Turbo-Frame: stats, and the route answers with only the frame. A plain visit gets the whole page around it
(routes/stats.tsx):
export const GET = defineHandler(async (c) => {
const stats = <Stats count={await countMessages()} />;
if (frameRequest(c) === "stats") return c.html(stats);
return c.html(<Layout c={c} title="Stats">{stats}</Layout>);
});
The answer must contain a <turbo-frame> with the same id. Turbo would take it out of a whole page too,
but answering with just the frame saves the work.
Navigating
- A link inside a frame loads into that frame.
data-turbo-frame="_top"on the link makes it load the whole page instead. data-turbo-frame="stats"on a link outside aims it at the frame.data-turbo-action="advance"makes a frame's navigation update the URL and history.- Without
loading="lazy", a frame withsrcloads at once.
A region that is only changed by streams does not need to be a frame. An element with an id is enough.
Reference: https://turbo.hotwired.dev/handbook/frames
Check
curl -H 'Turbo-Frame: stats' https://hotwire.localhost:5173/stats returns one <turbo-frame id="stats"> and nothing else.
When it does not work
| Symptom | Cause |
|---|---|
| "Content missing" in the frame | The answer has no <turbo-frame> with that id |
| A link inside a frame does nothing | It goes to a page without that frame; add data-turbo-frame="_top" |
| The frame never loads | No src, or loading="lazy" on a frame that never scrolls into view |