diff --git a/Guide/auto-refresh.markdown b/Guide/auto-refresh.markdown index 4877f175f..10bdf6b47 100644 --- a/Guide/auto-refresh.markdown +++ b/Guide/auto-refresh.markdown @@ -98,7 +98,6 @@ action MyAction = do -- <-- We don't enable auto refresh at the action start in render MyView { expensiveModels, cheap } ``` -### Custom SQL Queries with Auto Refresh Auto Refresh automatically tracks all tables your action is using by hooking itself into the Query Builder and `fetch` functions. @@ -124,3 +123,159 @@ action StatsAction = autoRefresh do ``` The [`trackTableRead`](https://ihp.digitallyinduced.com/api-docs/IHP-ModelSupport.html#v:trackTableRead) marks the table as accessed for Auto Refresh and leads to the table being watched. + +### Using Auto Refresh with HTMX + +HTMX and Auto Refresh work well together: + +- HTMX loads/replaces fragments in response to user interactions +- Auto Refresh keeps those fragments up-to-date when database rows change + +For pages that use HTMX fragments, include the HTMX-specific Auto Refresh client: + +```haskell +scripts :: Html +scripts = [hsx| + + + + + + |] +``` + +`/helpers-htmx.js` + `/ihp-auto-refresh-htmx.js` is the HTMX equivalent of the classic +`/helpers.js` + `/ihp-auto-refresh.js` setup: + +- `helpers-htmx.js` handles HTMX morphdom swaps and helper compatibility (`ihp:load`, `ihp:unload`, date/time formatting, flatpickr init, toggle/back/file preview helpers, alert dismiss on request) +- `ihp-auto-refresh-htmx.js` handles Auto Refresh WebSocket sessions, target-based fragment updates, and pause/resume around HTMX requests + +Keep this script order: + +1. `htmx.min.js` +2. `morphdom-umd.min.js` +3. `helpers-htmx.js` +4. `ihp-auto-refresh-htmx.js` +5. `app.js` + +Use `/ihp-auto-refresh.js` for full-page morphing without HTMX. +Use `/ihp-auto-refresh-htmx.js` when HTMX controls fragment swaps. +Do not include both scripts on the same page. +Also do not include both `/helpers.js` and `/helpers-htmx.js` on the same page. + +For HTMX fragment actions, prefer `renderFragment`. It skips the layout and includes the Auto Refresh meta tag. + +#### End-to-end example + +Let's say we want a project page where comments are loaded by HTMX and then kept live by Auto Refresh. + +In the parent page we render a target container with a stable `id`: + +```haskell +[hsx| +