Headers: _cache(), _cache_none(), _robot_none() htmx

Cache-Control and X-Robots-Tag of an htmx answer (yf_api): no-store for live data, none for fragments search engines must not index.

_class('htmx')->_cache($options = null)
_class('htmx')->_cache_none()
_class('htmx')->_robot_none()

Options

OptionMeaning
_cache($options)yf_api::_cache(): public / private, max_age, no_store...
_cache_none()Cache-Control: no-store.
_robot_none()X-Robots-Tag: none (_result() sends it itself).

Example

Code

$hx = _class('htmx');
if ($hx->_wants_fragment() && ($_GET['hx'] ?? '') === 'live') {
    $hx->_cache_none();
    $hx->_robot_none();
    return $hx->_result('Live value ' . random_int(100, 999) . ', sent with Cache-Control: no-store');
}
if ($hx->_wants_fragment() && ($_GET['hx'] ?? '') === 'cached') {
    $hx->_cache(['max_age' => 60]);
    return $hx->_result('Cacheable for 60 s: ' . date('H:i:s'));
}
asset('htmx');
$self = _prepare_html(url('/@object/htmx/cache'));
return '<button type="button" class="btn btn-default btn-outline-secondary" hx-get="' . $self . '" hx-vals=\'{"hx": "live"}\' hx-target="#docs-ref-hx-cache">No-store value</button> '
    . '<button type="button" class="btn btn-default btn-outline-secondary" hx-get="' . $self . '" hx-vals=\'{"hx": "cached"}\' hx-target="#docs-ref-hx-cache">Cacheable value</button> '
    . '<span id="docs-ref-hx-cache"></span>';

Notes

  • They send headers directly (not through SEND_HEADERS): call them only on the answer they are for, as the example does on its htmx requests.

See also: htmx/fragment