34 lines
1.6 KiB
JavaScript
34 lines
1.6 KiB
JavaScript
import './stimulus_bootstrap.js';
|
|
import './styles/app.css';
|
|
|
|
import htmx from 'htmx.org';
|
|
|
|
// Exposed so inline `hx-on:` handlers and Stimulus controllers can reach htmx.
|
|
window.htmx = htmx;
|
|
|
|
// We never return <script> tags in fragments; refuse to run them if one slips through.
|
|
htmx.config.allowScriptTags = false;
|
|
// Indicators are styled in Tailwind, not by htmx's injected <style> block.
|
|
htmx.config.includeIndicatorStyles = false;
|
|
|
|
// AbstractController::render() returns 422 when it is handed an invalid submitted form, and
|
|
// htmx 2 does not swap 4xx by default -- so without this entry a failed validation would
|
|
// silently do nothing and the modal would just sit there unchanged.
|
|
// resolveResponseHandling() returns the FIRST matching entry, so 422 must precede the
|
|
// [45].. catch-all. The other three entries are htmx's defaults, repeated because assigning
|
|
// this config replaces the whole array.
|
|
htmx.config.responseHandling = [
|
|
{ code: '204', swap: false },
|
|
{ code: '[23]..', swap: true },
|
|
{ code: '422', swap: true, error: false },
|
|
{ code: '[45]..', swap: false, error: true },
|
|
];
|
|
|
|
// Anything else that fails -- a 4xx other than 422, a 5xx, or no response at all -- reloads the
|
|
// page. htmx only opens modals and refreshes fragments, so there is always a whole page underneath,
|
|
// and every page is a GET: reloading is safe, closes the modal and shows current data. The server
|
|
// keeps honest status codes and leaves a flash explaining the reload (HtmxErrorFlashListener).
|
|
const reloadPage = () => window.location.reload();
|
|
document.addEventListener('htmx:responseError', reloadPage);
|
|
document.addEventListener('htmx:sendError', reloadPage);
|