Skip to content

HX-Location and HX-Redirect headers ignored during history restoration (back button navigation) #3432

@kohkimakimoto

Description

@kohkimakimoto

HX-Location and HX-Redirect headers ignored during history restoration (back button navigation)

When htmx loads history from the server using the loadHistoryFromServer function, response headers like HX-Location and HX-Redirect are ignored.
This causes authentication applications to display blank pages when users press the back button after login.

Affected use case

This issue occurs when using hx-history="false", which forces htmx to load history from the server.
The following is an example flow that triggers this issue:

  1. User displays login form
  2. User successfully logs in and gets redirected to dashboard page that requires authentication
  3. User presses browser back button
  4. Browser returns to login form URL, htmx loads history from server (calls the loadHistoryFromServer function)
  5. Server detects user is already authenticated, responds with HX-Location header to redirect to dashboard instead of returning login form HTML
  6. htmx ignores the HX-Location header and swaps the empty HTTP response body
  7. Result: the login form becomes blank instead of proper redirect

Suggested solution

Added header processing logic in the loadHistoryFromServer function that follows similar logic to the existing handleAjaxResponse function.

This ensures that HX-Location and HX-Redirect headers are properly handled during back button navigation, resolving the authentication flow issue that I described above.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions