Error page can't be swapped on history cache miss
As of HTMX 2.0.6, in case of a history cache miss loadHistoryFromServer() checks if the xhr status code falls between 200 and 400, and any error codes (>=400) triggers htmx:historyCacheMissLoadError. As far as I can see, this makes it impossible to return with a legitimate, informative error page with the semantically correct HTTP code and have any of the content swapped in.
In my specific situation, I allow swapping of error pages with responseHandling in the config, but codeMatches() is never used in loadHistoryFromServer().
Is this a bug or intended behaviour? Any advice or something obvious I missed?
Yes for legacy reasons the loadHistoryFromServer() has always defaulted to this basic status code checks and it has not been upgraded to use responseHandling config sorry. I recently rewrote this function but kept 100% backwards compatibility instead of improving this area.
The best solution for your use case is to jus set the htmx config refreshOnHistoryMiss to true which has a small performance hit when you push back too many times and cause a cache miss but it makes htmx perform a full page load instead of a htmx ajax body replacement. history loads are always basically full page loads anyway so this normally works well.
You can also override the handling of any of the history features with event listeners now.
document.body.addEventListener('htmx:historyCacheMissLoadError', function(evt) {
console.log(evt.detail) // do something with evt.detail.xhr.response
}
Thank you for the thorough response. I now combine refreshOnHistoryMiss with hx-history="false" for error pages, and it gets closer to what I had in mind.