HX-Redirect and HX-Refresh of the framework htmx

How the framework answers htmx where a page would redirect: HX-Redirect after an admin action, 401 + HX-Refresh for a logged-out admin.

Options

OptionMeaning
yf_redirect::$HTMX_REDIRECTAn htmx request (not boosted) gets HX-Redirect to the target (200, no body) instead of the redirect page or a 302 that the XHR would follow and swap in. Off by default; admin_post::refusal() turns it on for the Confirm of its htmx form.
logged-out adminAn htmx request of a logged-out admin gets 401 + HX-Refresh: true + no-store and no body: htmx reloads the page, which shows the login form and returns there after login (yf_tpl::_hx_login_refresh()).
background requestshtmx, ajax, prefetch and similar requests are never remembered as the page to return to after login.

Example

Without HX-Redirect the redirected page would be swapped in here.

Code

$hx = _class('htmx');
if ($hx->_wants_fragment() && ($_GET['hx'] ?? '') === 'go') {
    // what a module does after a change: redirect; with HTMX_REDIRECT on, htmx gets HX-Redirect and loads the page
    $redirect = _class('redirect');
    $redirect->HTMX_REDIRECT = true;
    return $redirect->_go(url('/@object/htmx/framework_redirects') . '#redirected', false);
}
asset('htmx');
return '<p>' . html()->button('Run an action that redirects', ['type' => 'primary', 'attr' => ['hx-get' => url('/@object/htmx/framework_redirects'), 'hx-vals' => '{"hx": "go"}', 'hx-target' => '#docs-ref-redir']]) . '</p>'
    . '<div id="docs-ref-redir" class="well">Without HX-Redirect the redirected page would be swapped in here.</div>';

Notes

  • Both are BC changes of 2026-11-15 (no flag, bug fixes): see UPGRADING. Plain ajax (X-Requested-With) keeps its 403 without a body.
  • Your own module: _class('htmx')->_redirect($url) or _refresh() (redirect helpers).

See also: htmx/redirect, htmx/admin_confirm, misc:admin_login