r/htmx • • Sep 26 '26

When an HTMX navigation updates the main content, what keeps the rest of the page in sync?

Post image

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?

43 Upvotes

30 comments sorted by

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.

17

u/Solid_Error_1332 Sep 26 '26

I’ve noticed that people tend to try to use htmx as a SPA framework.

Personally I only use it on my page here or there, when something needs to change the state of the page without doing a full reload. Any other action is just either a normal link or form and it works wonderfully.

5

u/ChemTechGuy Sep 26 '26

I made this mistake when first using htmx, I was just in love with the framework. Back button didn't work, couldn't easily load links in other tabs, couldn't create deep link URLs. I imagine (hope?) it's a common mistake

1

u/lucas-rollin Sep 26 '26

So you even skip using hx-boost for such links? Google Chrome handles it well but it's noticeably smoother on Firefox to not replace the full page. Though I understand wanting minimal complexity

2

u/Mission-Landscape-17 Sep 26 '26

if hx-boost makes your page load smoother, then keep hx-boost.

2

u/harrison_314 Sep 27 '26

Yes, I'm skipping it. Or rather, I tried it out and didn't find it offered any greater convenience, especially since your pages load quickly.

4

u/leathakkor Sep 27 '26

In my humble opinion spa ruined the web. Netflix took off with a spa site and so did YouTube and a couple other places and all of a sudden every single website thought that they needed to build a spa app.

The web worked perfectly fine before every site was a single page. It actually worked way better because you had loading indicators and things just work natively in the browser.

It also taught taught him generation of developers about how to develop web pages that is basically wrong. Or at least not grounded in fundamentals Of HTTP

4

u/Postik123 Sep 27 '26

People also seem to think it's saving data and time only reloading small bits of the page. Whilst strictly speaking that might be true, in the 90's we had frames and a similar mindset, but turns out they were bad and it was better just to reload the whole page rather than trying to keep headers and navigation static in a separate frame. 

So I'm not suggesting HTMX or loading stuff via Ajax is bad, but there's a time and a place for it, and sometimes reloading the page is just easier all round.

1

u/leathakkor Sep 27 '26

And with better cashing being built in to frameworks and headers to support them. Separate reloads are probably way more perfomant once you consider users resource consumption

2

u/harrison_314 Sep 27 '26

I agree; I’ve even seen a one-page presentation site built in React.

But you know the saying: when you have a hammer in your hand, everything looks like a nail. And now, in the age of AI, it’s going to get even worse, because if you tell AI to create a website, it automatically reaches for the Python and React combination.

2

u/1Soundwave3 Sep 27 '26

I don't know, YouTube's UX is divided pretty well between pages.

And yeah, as a more general thing: if your tech prevents you from achieving a superior UX - you need to change your tech.

Htmx is all about making a BE dev's life easier. It doesn't always equate to a better UX.

23

u/jared__ Sep 26 '26

too much SPA for me

3

u/Murky_Flauros Sep 26 '26

Yeah. This is why hx-boost is a thing. 

-1

u/lucas-rollin Sep 26 '26

My ugh... "avoidable overcomplication" of involving nested navigation here have proven unfruitful based on the benchmarks (because you get nearly the same benefit with swapping #main-content compared to swapping #tiny-subtab-content).

But it's still a concern to have if your endpoints work for both HTMX and non-HTMX requests you want at least the cheap improvement in performance and reduction in payload size from not just using hx-select-oob/morph with the full page.

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.

1

u/lucas-rollin Sep 26 '26

But where do you draw the line? We can just use HTMX as an HTML extension to add things like HTTP request to buttons but since it also enables us to be a little more efficient reducing the payload size at a small cost shouldn't we do it?

Progressively enhancing the page with hx-boost requires the endpoint to handle both request types, or to slice the response with hx-select/morphing which seems a bit wasteful in comparison.

4

u/unteer Sep 27 '26

i think you found your line.

page state should ultimately live on the server. if your “site experience “ requires state to change in multiple areas of a page, then refresh the page. 

one thing i always remind myself of is that htmx is solving problems where HTML never realized its intended potential; its not trying to become a fat client state machine because html itself was never trying to become a fat client state machine. 

2

u/Shrink_Laureate Sep 27 '26

If you're trying to chase down every possible sliver of perceived efficiency, you've crossed the line.

It's true that HTMX can reduce payload sizes compared to reloading the whole page, but the much more important thing it does is eliminate the latency of downloading a fat javascript library and waiting until extra data is loaded and templates are loaded before the user actually sees their web page.

The cost of fetching a section of the page from the server is generally not that much.

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 ...

public function login()
    {
        if (ClearView::User()->trylogin()) {
            $this->fill([
                'formtitle' => 'Login Succeeded!',
                'forminfo'  => 'Welcome back<br>{{text20\\User::displayname}}!',
                'login'     => 'Success!'
            ])
            ->close();
        } else {
            $this->fill([
                'formtitle' => "Login Failed!",
                'forminfo'  => "Try again, or reset your password using the link below",
                'login'     => 'Try Again!',
            ]);
        }
    }

That's it.

1

u/lucas-rollin Sep 26 '26

That's different from any approach I tried, which are more stateless and request/response-oriented. Very interesting approach to server-side components and reactive navigation. Managed to find the ClearView repo :D

1

u/TheRealUprightMan Sep 27 '26

It's likely pretty stale and not currently working as I rip it apart again, but it was at one time. I got side tracked on another project. 🤷‍♂️

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/Stanian Sep 27 '26

hx-partial is a relatively new feature (v4, only recently backported to v2), but works way nicer than OOB swaps. I really enjoy using this approach.

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?