Hypa

status-badgeHex.pm VersionHex.pm Licensehexdocs.pm

Useful functions for Elixir applications using htmx.

Offers a Plug that performs a few tasks:

Installation

This library is available in Hex, and the package can be installed by adding hypa to your list of dependencies in mix.exs:

def deps do
  [
    {:hypa, "~> 0.2.0"}
  ]
end

Documentation can be found at https://hexdocs.pm/hypa.

Usage

Add Hypa.Plug to your Plug pipeline like this:

plug Hypa.Plug

Request headers

This plug adds htmx's request headers to conn.assigns:

header assign
HX-Boostedconn.assigns[:htmx][:boosted]
HX-Current-URLconn.assigns[:htmx][:current_url]
HX-History-Restore-Requestconn.assigns[:htmx][:history_restore_request]
HX-Promptconn.assigns[:htmx][:prompt]
HX-Requestconn.assigns[:htmx][:request]
HX-Targetconn.assigns[:htmx][:target]
HX-Trigger-Nameconn.assigns[:htmx][:trigger_name]
HX-Triggerconn.assigns[:htmx][:trigger]

If the request does not have htmx headers, then conn.assigns[:htmx] will not be present.

Response headers

This plug also adds a Vary response header to your conn containing HX-Request, because your server is likely rendering different content for htmx requests. This value is prepended to the values already in the Vary header, so it will not overwrite any values you've already put in there. If you want to add more values to the Vary header yourself, be sure to use Plug.Conn.prepend_resp_headers/2 unless you do intend to overwite the entire Vary value.

header value
varyhx-request

Phoenix layouts

If your project is using Phoenix, Hypa.Plug will also disable Phoenix's root and app layouts when responding to htmx requests. If the htmx request is boosted or is a history restore request, however, the app layout will be enabled, since those requests expect an entire HTML body. The root layout is disabled for all htmx requests.

condition root layout app layout
HX-Request != "true" enabled enabled
HX-Boosted == "true" disabled enabled
HX-History-Restore-Request == "true" disabled enabled
HX-Request == "true" disabled disabled

License

Copyright © 2025 Rosa Richter

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License along with this program. If not, see https://www.gnu.org/licenses/.