Boosting Links — HTMX

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…

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>

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>

What still reloads, or isn't boosted

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

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.

What you will learn