Mrs. Rini's laundry site is done, with a Board and a Report menu made of plain <a href> links. Every time a menu item is clicked, the browser loads a new document from scratch: its <head> is thrown away, the CSS and scripts are processed again. You don't have to rewrite every link as hx-get. One attribute on their wrapper is enough.
hx-boost="true" makes the ordinary links and forms inside it go through htmx: a request in the background, then the content of <body> is swapped, with no full page reload. It's like a ring binder on Mrs. Rini's counter. Without boost, every time she needs another page, the whole binder is thrown away and a new one is fetched from the storeroom: cover, rings and sheets. With boost, the binder stays and only the sheets inside are swapped. The new sheets still arrive complete from the storeroom, but the cover and rings aren't replaced.
One attribute for every link
<body hx-boost="true">
<header>
<h1>Mrs. Rini's Laundry</h1>
<nav>
<a href="/board">Board</a>
<a href="/report">Report</a>
</nav>
</header>
<main id="main">...</main>
</body>
hx-boostis inherited: every<a>and<form>inside that element is boosted. Put it on<body>for the whole site, or on<nav>for just the menu.- Write the value in full,
hx-boost="true". - The links keep their plain
href. If htmx fails to load, the links still work as usual. This is progressive enhancement: the page still works without JavaScript, htmx only makes it smoother.
What happens when Report is clicked
| Step | Without hx-boost |
With hx-boost="true" |
|---|---|---|
| Request | the browser loads the /report document |
htmx sends GET /report with the headers HX-Request: true and HX-Boosted: true |
| Reply | a full page | the same full page, the server needs no change |
<head> |
thrown away, CSS and scripts processed again | the old one stays. From the new one, only <title> is taken |
<body> |
the page is drawn from scratch, the screen flashes white | the old <body> content is swapped for the new <body> content (innerHTML) |
| Address | /report |
htmx writes /report to the address bar with history.pushState |
In the Lab, without boost one click loads 3 files: the document, app.css and htmx.min.js (the last two often come from the cache, but they're still processed again). With boost it's 1 request. The page also scrolls to the top, like a normal page change. Scripts inside the new <body> are still run by htmx, but the <link> and <script> tags in the new <head> are ignored.
Forms are boosted too
<form id="intake" action="/order" method="post">
<input name="name" placeholder="Name">
<input name="weight" placeholder="kg">
<button>Take in laundry</button>
</form>
- Inside
<body hx-boost="true">, htmx sends this form asPOST /orderwith the bodyname=Mrs.%20Tini&weight=4. Nohx-postneeded: htmx uses theactionandmethodalready there. - The Laravel route saves the order, then
return redirect('/board'). The browser follows that 302 redirect by itself, so htmx receives the/boardpage that already holds #15 Mrs. Tini, swaps its body in, and writes/boardto the address bar.
What still reloads, or isn't boosted
- Links to another site, such as
https://wa.me/...: the host is different. - Links that start with
#, such as#prices: that's a jump within the same page. - Links with
target="_blank", and Ctrl+click or Cmd+click: they open in a new tab as usual. - Links or forms with
hx-boost="false". Use it for downloads like/prices.pdf, because when boosted, htmx puts that file's content into the body as text. - Refresh (F5), or opening an address directly: the browser loads the whole page from the server. That's why
/reportmust still be able to answer with a full page.
The Back button
Before it writes a new address, htmx saves a copy of the current page (the body content, the title, the scroll position) in sessionStorage, up to 10 pages. When you press Back, the browser fires a popstate event, and htmx puts that copy back. No request at all: 0.
If there's no copy, for example because the tab was just opened at /report, htmx asks the server for the page with the header HX-History-Restore-Request: true, and the server answers with the full page.
The copy in sessionStorage is plain HTML, including private data such as the daily takings on the Report page. If the cashier logs out through a boosted link, the next person can press Back in that tab, and the Report shows again from the copy without asking the server. For such a page, put hx-history="false" on any element of the page: htmx doesn't store its copy, and Back asks the server for the page again. To turn it off for the whole site: <meta name="htmx-config" content='{"historyCacheSize": 0}'>. Both were tested in Chromium with htmx 2.0.11.
⚠️ When a boosted link acts strangely
- The server answers every
HX-Requestwith a fragment. Boosted requests carryHX-Request: truetoo. If the/boardroute returns only an<li>fragment when that header is there, the boosted Board link puts that fragment in as the whole body. CheckHX-Boostedas well, or answer with the full page. - An error reply makes a click feel dead. A 404 or 500 reply isn't swapped, so the page sits still. Check the Network tab.
- A download link inside a boosted area. Give it
hx-boost="false". - The wrong value or place in the exercise. Write
hx-boost="true"on the wrapper that really holds all three links. In the form exercise,hx-boost="true"goes on the<form>itself, withaction="/login",method="post", and two inputs with aname.
TL;DR: hx-boost="true" on <body> or <nav> makes every local link and form inside it go through htmx: a GET with the header HX-Boosted: true (a form uses its action and method), the server answers with a full page as usual, htmx takes its <title>, swaps the <body> content, and writes the new address with pushState. The old <head> stays, so CSS and scripts aren't processed again. The Back button restores a copy from sessionStorage with no request. Links to another site, #..., target="_blank", Ctrl+click and hx-boost="false" work as usual, and without htmx everything still works.
⚡ One attribute for every link
Write:
<body hx-boost="true">
<header>
<h1>Mrs. Rini's Laundry</h1>
<nav>
<a href="/board">Board</a>
<a href="/report">Report</a>
</nav>
</header>
<main id="main">...</main>
</body>
Meaning: hx-boost is inherited: every <a> and <form> inside it goes through htmx. The links keep their plain href, so everything still works without htmx.
⚡ Only the body content is swapped
Guess first: the boosted Report link is answered with a full page. Which part does htmx swap in?
The content of its <body>, plus the new <title>. The old <head> stays, so CSS and scripts aren't processed again.
Meaning: htmx sends GET /report with the header HX-Boosted: true, then writes /report to the address bar with pushState. The server needs no change.
⚡ Forms are boosted too
Write:
<form id="intake" action="/order" method="post">
<input name="name" placeholder="Name">
<input name="weight" placeholder="kg">
<button>Take in laundry</button>
</form>
Meaning: Inside <body hx-boost="true">, this form is sent as POST /order with no hx-post, because htmx uses its action and method. The Back button restores a copy from sessionStorage, with no request.