Top
Best
New

Posted by leephillips 11/3/2025

</> Htmx – The Fetch()ening(htmx.org)
361 points | 153 commentspage 2
logankeenan 11/3/2025|
This is great! I had to create an xhr-fetch-proxy to use fetch and htmx. This change should open up a lot of fun possibilities.

https://github.com/logankeenan/xhr-fetch-proxy

recursivedoubts 11/3/2025|
in 4.0 the request cycle is totally open: you can replace the fetch() implementation on a per-request basis

learned that trick from fixi.js

logankeenan 11/3/2025||
Yep, it's super nice. The Service Workers API also makes this really easy too. I experimented with compiling a Rust Axum server to WASM and then ran it in my service worker. Also, thanks for incorporating fetch into htmx!

I thought I'd include an example of replacing fetch for anyone that come across this.

    const originalFetch = window.fetch;
    window.fetch = function(url, options) {
      if (url.includes('/api/user')) {
        const mockUser = {id: 1, name: 'John Doe', email: 'john@example.com'};
        return Promise.resolve(new Response(JSON.stringify(mockUser)));
      }
      return originalFetch(url, options);
    };
https://developer.mozilla.org/en-US/docs/Web/API/ServiceWork...
recursivedoubts 11/4/2025||
in htmx 4 you are able to swap it on a per-trigger basis, so need to muck w/the global fetch function:

   <button hx-get="/foo" hx-on:htmx:config:request="ctx.fetch = myCustomFetch">
     Do It
   </button>
logankeenan 11/4/2025||
Oh that’ll be much nicer. Would myCustomFetch then need to return a Response?

https://developer.mozilla.org/en-US/docs/Web/API/Response

masfoobar 11/4/2025||
Computer works in binary

htmx 1 htmx 2 htmx 4

I look forward to version 8 in 2030.

(Meaning there always was a version 3! Just include both htmx1 and htmx2 onto your webpage!)

huntercaron 11/4/2025||
> In 4.0, with the complexity savings we achieved by moving to fetch(), we can now comfortably fit a morphInner and morphOuter swap into core

It seems odd to me to treat these as different properties on their own? might as well keep it as an extension if it is not going to be more deeply integrated and let htmx 4.0 be smaller? Possible I may not be understanding the use case though

adamzwasserman 11/3/2025||
Interesting timing - I'm building genx.software which leans hard into implicit inheritance for the exact reason he is abandoning it. In my finance app, when the product owner changes their mind about decimal places for the 47th time (and it's always 47), the last thing I want is to hunt down every cell and add :inherited modifiers.

We treat everything as integers server-side (because floating point is the devil) and defer all formatting to the client. Implicit cascade means I can change fmt-x="currency:USD:decimals:2" in one place and watch it ripple down the entire table. It's 'maddening' the way CSS is maddening - which is to say, it becomes second nature after you stop fighting it.

That said, I fully understand the support ticket burden. Maybe the real lesson is: implicit inheritance is great when you control the domain (like 'format all money the same way'), but terrible when people want to do arbitrary things at arbitrary levels. I will bear that in mind as I complete genx.software

amanzi 11/3/2025||
This looks great, although the version 4 alpha doesn't appear to be available at the suggested URL: https://cdn.jsdelivr.net/npm/htmx.org@4.0.0-alpha/dist/htmx....

"Couldn't find the requested release version 4.0.0-alpha."

ian-g 11/3/2025|
It's named 4.0.0-alpha1. Just add the 1 and the link works
amanzi 11/3/2025||
Thanks. The docs need to be updated at https://four.htmx.org/
ranger_danger 11/3/2025||
The only big gripe I have about htmx is that the hx-on::after-request response it provides to your callback function does not automatically parse JSON content types like with e.g. jQuery.ajax(). Last time I brought that up, people simply questioned why I would ever want to do that in the first place.
recursivedoubts 11/3/2025||
in 4.0 we are opening up the entire request/response/swap mechanism so you can replace any component of it per-trigger

you can replace the fetch() function used w/ an event callback, etc

should allow you to do pretty much anything w/o any hacks

edoceo 11/3/2025||
That sounds like use-case exploration? Did you answer?
ranger_danger 11/3/2025||
One of the actual responses was "Htmx isn’t designed to work with JSON APIs at all. It needs HTML back from the server."

It sounds like they are referring to hx-swap and not arbitrary javascript callbacks though, and in that case, I don't see why calling JSON.parse() inside htmx (when the content-type is json) is that big of a deal.

isleyaardvark 11/3/2025|||
It's a pretty core part of their design philosophy, possible the core.

https://htmx.org/essays/rest-explained/

sudodevnull 11/4/2025||
So you can support JSON while still being REST. For example, Datastar supports merging in HTML, JS, JSON into the current view of a resource. They work together to keep the resource state unified versus polling. In general the Datastar way is...

1. make an MPA 2. each page is a resource 3. keep a stream open on the current state of that resource 4. ship, touch grass, repeat

johannes1234321 11/3/2025||||
They don't want to become a general purpose dynamic web library, but focus on swapping server generated HTML blocks. It's a conscious decision in what they are and what not.
bananapub 11/4/2025|||
> One of the actual responses was "Htmx isn’t designed to work with JSON APIs at all. It needs HTML back from the server."

Uh, yes? They wrote a literal book about why they think this is important: https://hypermedia.systems/

causal 11/3/2025||
Neato. Sensible updates, no loss of support for previous versions? The software dream.
ksec 11/5/2025||
Hopefully one day we will see htmx 4.0 being merged into HTML Spec.
prokopton 11/4/2025||
htmx hasn’t had the piss taken out of it enough.
recursivedoubts 11/4/2025|
100%
epolanski 11/4/2025|
The image breaks the website on mobile for me.
More comments...