Bwf/ GuideFrançais
Bwf Guide

HTMX and fragment responses

Open the feed in a fresh tab, then click Next. The second page only needs a new list of articles. We’ll use the same URL whether the reader follows the link or pastes it into another tab.

Keep an address for the full page

Give each page of results an ordinary link. When a GET arrives, its headers tell the Journal whether to send the whole document or just the region HTMX will replace.

Phoenix LiveView

Add the client and declare a fragment

Add Htmx.assets() to the head in your layout. Next, look at the operation serving the feed. When it declares x-bwf-htmx: true, its action takes fragment: Bool. Use this argument to pick the full document or fragment response declared for the operation.

Put the helper in the document head.

{Htmx.assets()}

Name the element to replace

Start with a link you can open in its own tab. Add hx-boost to enhance that link or a regular form. If you write hx-get or hx-post explicitly, build its destination with the typed url(…). Inspect the rendered page once and check your target selector against the element you mean to replace.

<a href={url("/items/{id}")} hx-boost="true" hx-target="#item">View item</a>

Compare the two responses

Send an ordinary GET, then send HX-Request: true. Inspect both bodies and the Vary header. A history restore should get the full document. Also open the URL directly after testing the click.

These two requests compare the Journal feed in the terminal.

curl -si http://127.0.0.1:18088/
curl -si -H 'HX-Request: true' http://127.0.0.1:18088/

Try it in the Journal

Update the Journal in place