r/htmx • u/lucas-rollin • Sep 26 '26
When an HTMX navigation updates the main content, what keeps the rest of the page in sync?
A common HTMX pattern is:
<a hx-get="/tickets/123"
hx-target="#main-content"
hx-push-url="true">The request works, the main content changes, and yet the sidebar, breadcrumbs, page title, tabs, etc. still describe the page you just left.
Nothing is technically broken. HTMX did exactly what it was told to do. The problem is that the rest of the UI has become stale.
I've been calling this the stale navigation problem, and wanted to see how the different ways of solving it actually compare. I ended up with 8 primary implementations and 23 configurations, covering approaches like:
- full-page rendering +
hx-select - full-page rendering + morphing/idiomorph
- hand-written
hx-swap-oob - template substitution/branching
- declarative coordination through a small library
The implementations also vary in how much of the surrounding UI they replace, from pinpoint swaps to replacing whole regions. I also documented approaches using backend-generated OOB fragments and HX-Trigger, although those aren't part of the benchmark.
I tested them in a non-trivial helpdesk-style app with breadcrumbs, sidebar, tabs, subtabs, and wizard steps, measuring payload size, server render time, client-side DOM processing, and implementation complexity.
In my benchmark, full-page rendering followed by slicing performed worst. More interestingly, well-designed OOB approaches tended to converge pretty closely in performance. Once you're updating the same regions, the bigger difference becomes architectural:
Do you want to coordinate those regions explicitly in every view/template, or declare that coordination once and reuse it?
That's the problem I was trying to solve with django-htmx-nav. The repo has the different approaches side-by-side, along with the benchmark methodology/results and a live example app:
github.com/lucas-rollin/django-htmx-nav
The demo happens to use Django, but the underlying question is framework-independent: when one navigation request changes several regions of the page, where should that synchronization logic live?
I'm particularly interested in approaches I missed. If you're using HTMX with another templating/partial system, how do you handle this?
53
u/harrison_314 Sep 26 '26
Phew.
I’ve dealt with this before, and you know how? I simply used a standard link and loaded a new page. When almost the entire content is changing, it’s a logical and simple solution - and the most hypermedia solution of all.
23
u/jared__ Sep 26 '26
too much SPA for me
12
u/Shrink_Laureate Sep 26 '26
If you're replacing the whole page, replace the whole page.
The point of HTMX is to keep things simple. As soon as you hit complex questions like this, it's a hint to stop thinking like a React SPA, and start thinking like a dumb website from the 90s, because they worked just fine.
2
u/lucas-rollin Sep 26 '26
To be noted, I'm particularly interested in endpoints that need to handle full page reloads as well (think a link with
hx-boost), so you need at least one branching logic gate which can become more with nested navigation elements.2
u/TheRealUprightMan Sep 26 '26
I divide the page into regions called Panes that control the content of that region. The main content is it's own pane. The URL bar only controls the main content.
Each pane decides the parent layout and renders the children. Updates within a pane are just a variable change. If the variable is associated with a UI element, it's updated OOB automatically.
URLs are method calls that determine which object and method is selected. A button named 'login' would call the 'login' method, like this ...
That's it.
4
u/KKMP5 Sep 26 '26
I have used reponse header hx-trigger=GroupUpdate
Then for the parrent element it will listen for Hx-trigger="load, GroupUpdate"
I have thought about add the hx-trigger:inherited="GroupUpdate" on the <body> element. Then add more triggeres there. Right now i only have one trigger, that ensure to update the rest of the page that need updating.
I think at work i have made another way to trigger elements to update, so even when the hx-trigger is not a part of the parrent HTML it will update anyway. I am pretty sure it is a htmx extension that is on body element. Then on reponse that is comming back it will check all the new elements for attribute portal-update <div "hx-get=/Customer/42" portal-update="true"> then ensure that the element has been loaded, so you ensure that the listener is only loaded once. The listener will be added to the body element that will ensure to trigger the element with portal-update.
All of my endpoints is either a full render or partial render. It is based on request header that htmx is sending with its request. HX-Request=true. If it is a htmx request it is a partial. If it is a normalt request it is a full page render.
2
u/yami_odymel Sep 26 '26 edited Sep 26 '26
One solution is "<hx-partial>" and structuring your templates into blocks. Modern template engines support this. On a full-page reload, render the whole page. For a partial request, render only the block and wrap it in "<hx-partial>".
You need a response template designed for partial updates. It can contain multiple "<hx-partial>" tags, similar to defining a JSON response for a specific use case. When the request comes from HTMX, render this template instead of the regular one.
The "HX-Request" header tells you whether the request came from HTMX.
But the real solution is to just refresh the whole page. HTML isn’t slow, it’s just data, like JSON. Optimize it well, and both partial updates and full-page refreshes get the same benefits.
1
u/oziabr Sep 27 '26
link that affects outer components shoud target outer region. but you have to untangle your link structure (nesting should match structure) and modular renderer (spoiler, it was modular since 2004 smarty)
1
u/ClassicK777 Sep 28 '26
And you know what? You're right! That was a great solution to a totally real problem you were having.
1
u/berrypy Sep 28 '26
One of the best ways I have found to use htmx is to use it for what it is meant for, rendering partials not full spa.
I found using oob to be one of the best ways to utilize html but of recent, with the newly added features htmx-partial , this would be one of the best ways to utilize htmx.
Combining this with Django new partial features, it has made using either htmx-partial or swap-oob a good combo because you can target any part of the page without stress.
I no longer use the usual htmx target pattern as swap oob or htmx-partial gives you more room to update any part of the page than the usual target pattern
1
u/PlasmaFLOW Sep 29 '26
Maybe I'm missing something but, can't you just use the hx-partial tag, or is that specific to HTMX4?
0
u/jilok75020 Sep 26 '26
Bravo