Skip to content

http: stop treating history restoration as an HTMX request - #98

Merged
rameel merged 2 commits into
mainfrom
history-restore
Oct 13, 2026
Merged

rameel merged 2 commits into
mainfrom
history-restore

Conversation

@rameel

@rameel rameel commented Oct 13, 2026 •

Copy link
Copy Markdown
Owner

HTMX treats history restoration as a full page load, not as an HTMX request bigskysoftware/htmx#4139. HTMX 4.x sends this request without HX-Request, and so does HTMX 2.x with historyRestoreAsHxRequest: false. HTMX 1.9.x, and HTMX 2.x by default, still send the header, so IsHtmxRequest() gave different answers for the same request depending on the version.

With HTMX 1.9.x and 2.x, after a history cache miss:

return Request.IsHtmxRequest()
    ? PartialView("_List")
    : View(); // <-- not reached on history restoration
  • the whole page is replaced with a fragment
  • [HtmxRequest] without RequestType = Partial selects the HTMX-only action instead of the page
  • Response.Htmx(...) and [HtmxResponse] add headers that HTMX ignores during history restoration.

Fix:
IsHtmxRequest() now returns false when HX-History-Restore-Request is true, so history restoration is not an HTMX request in any version. [HtmxRequest], Response.Htmx(...) and [HtmxResponse] follow it. HtmxRequestHeaders.Request still reports the HX-Request header alone.

HTMX treats history restoration as a full page load, not as an HTMX
request: HTMX 4.x omits HX-Request on it, and HTMX 2.x does the same
when historyRestoreAsHxRequest is false. HTMX 1.9.x, and HTMX 2.x by
default, still send the header, so IsHtmxRequest() returned true for
the same request in some versions and false in others. With the
common IsHtmxRequest() ? Partial(...) : View() pattern, HTMX 1.9.x
and 2.x got a fragment in place of the page after a history cache
miss.

IsHtmxRequest() now returns false when HX-History-Restore-Request is
true, so history restoration is not an HTMX request in any version.
[HtmxRequest], Response.Htmx(...), and [HtmxResponse] follow it.
HTMX 1.9.x and 2.x do not process response headers when they restore
history, so skipping them loses nothing. HtmxRequestHeaders.Request
still reports the HX-Request header alone.

bigskysoftware/htmx#4139 (comment)
@rameel
rameel merged commit 573d0b4 into main Oct 13, 2026
1 check passed
@rameel
rameel deleted the history-restore branch October 13, 2026 16:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant