| 1 | --- |
| 2 | includeMockServer: true |
| 3 | title: "Lazy Load" |
| 4 | description: Load content after the page renders |
| 5 | icon: "icon-[bitcoin-icons--visible-filled]" |
| 6 | --- |
| 7 | |
| 8 | <script> |
| 9 | server.get("/demo", () => ` |
| 10 | <div hx-get="/weather" hx-trigger="load" hx-swap="outerHTML"> |
| 11 | <div class="size-full flex items-center justify-center gap-2 text-sm starting:opacity-0 transition"> |
| 12 | <span class="inline-block size-4 border-2 border-neutral-200 dark:border-neutral-700 border-t-neutral-400 dark:border-t-neutral-400 rounded-full animate-spin"></span> |
| 13 | Loading forecast… |
| 14 | </div> |
| 15 | </div>`); |
| 16 | |
| 17 | server.get("/weather", () => ({ delay: 300, body: ` |
| 18 | <div class="size-full"> |
| 19 | <div class="starting:opacity-0 starting:translate-y-1 transition flex items-center gap-2 mb-4"> |
| 20 | <span class="text-xl">☁</span> |
| 21 | <span class="text-sm font-semibold text-neutral-700 dark:text-neutral-200">5-Day Forecast</span> |
| 22 | </div> |
| 23 | <div class="grid grid-cols-5 gap-1 text-center"> |
| 24 | <div class="flex flex-col items-center gap-1 py-3 px-2 rounded-md starting:opacity-0 starting:translate-y-1 hover:bg-neutral-50 dark:hover:bg-neutral-850" style="transition:opacity 300ms ease-out 50ms,transform 300ms ease-out 50ms,background-color 150ms ease 0ms"><span class="text-xs uppercase tracking-wide text-neutral-500 dark:text-neutral-400">Mon</span><span class="text-lg font-semibold text-neutral-800 dark:text-neutral-100">72°</span><span class="text-xs text-neutral-500 dark:text-neutral-400">Sunny</span></div> |
| 25 | <div class="flex flex-col items-center gap-1 py-3 px-2 rounded-md starting:opacity-0 starting:translate-y-1 hover:bg-neutral-50 dark:hover:bg-neutral-850" style="transition:opacity 300ms ease-out 100ms,transform 300ms ease-out 100ms,background-color 150ms ease 0ms"><span class="text-xs uppercase tracking-wide text-neutral-500 dark:text-neutral-400">Tue</span><span class="text-lg font-semibold text-neutral-800 dark:text-neutral-100">68°</span><span class="text-xs text-neutral-500 dark:text-neutral-400">Cloudy</span></div> |
| 26 | <div class="flex flex-col items-center gap-1 py-3 px-2 rounded-md starting:opacity-0 starting:translate-y-1 hover:bg-neutral-50 dark:hover:bg-neutral-850" style="transition:opacity 300ms ease-out 150ms,transform 300ms ease-out 150ms,background-color 150ms ease 0ms"><span class="text-xs uppercase tracking-wide text-neutral-500 dark:text-neutral-400">Wed</span><span class="text-lg font-semibold text-neutral-800 dark:text-neutral-100">65°</span><span class="text-xs text-neutral-500 dark:text-neutral-400">Rain</span></div> |
| 27 | <div class="flex flex-col items-center gap-1 py-3 px-2 rounded-md starting:opacity-0 starting:translate-y-1 hover:bg-neutral-50 dark:hover:bg-neutral-850" style="transition:opacity 300ms ease-out 200ms,transform 300ms ease-out 200ms,background-color 150ms ease 0ms"><span class="text-xs uppercase tracking-wide text-neutral-500 dark:text-neutral-400">Thu</span><span class="text-lg font-semibold text-neutral-800 dark:text-neutral-100">70°</span><span class="text-xs text-neutral-500 dark:text-neutral-400">Partly cloudy</span></div> |
| 28 | <div class="flex flex-col items-center gap-1 py-3 px-2 rounded-md starting:opacity-0 starting:translate-y-1 hover:bg-neutral-50 dark:hover:bg-neutral-850" style="transition:opacity 300ms ease-out 250ms,transform 300ms ease-out 250ms,background-color 150ms ease 0ms"><span class="text-xs uppercase tracking-wide text-neutral-500 dark:text-neutral-400">Fri</span><span class="text-lg font-semibold text-neutral-800 dark:text-neutral-100">74°</span><span class="text-xs text-neutral-500 dark:text-neutral-400">Sunny</span></div> |
| 29 | </div> |
| 30 | </div>` })); |
| 31 | server.start("/demo"); |
| 32 | </script> |
| 33 | |
| 34 | <div id="demo-content" class="not-prose demo-container flex justify-center min-h-[218px]"></div> |
| 35 | |
| 36 | ## Basic usage |
| 37 | |
| 38 | On the client, put a placeholder where the content will go. |
| 39 | |
| 40 | ```html |
| 41 | <div hx-get="/weather" hx-trigger="load"> |
| 42 | Loading... |
| 43 | </div> |
| 44 | ``` |
| 45 | |
| 46 | - [`hx-get`](/reference/attributes/hx-get)=`/weather` might query a database or call an external API. |
| 47 | - [`hx-trigger`](/reference/attributes/hx-trigger)=[`"load"`](/reference/attributes/hx-trigger#load) fires the request as soon as the element enters the DOM. |
| 48 | |
| 49 | On the server, respond with the HTML content. htmx swaps it into the placeholder when it arrives. |
| 50 | |
| 51 | ```html |
| 52 | <div class="forecast"> |
| 53 | <h3>5-Day Forecast</h3> |
| 54 | <p>Monday: 72° Sunny</p> |
| 55 | <p>Tuesday: 68° Cloudy</p> |
| 56 | </div> |
| 57 | ``` |
| 58 | |
| 59 | ## Multiple sections |
| 60 | |
| 61 | Each placeholder loads independently. Your page renders fast even if several sections are slow: |
| 62 | |
| 63 | ```html |
| 64 | <div hx-get="/dashboard/sales" hx-trigger="load"> |
| 65 | Loading sales... |
| 66 | </div> |
| 67 | |
| 68 | <div hx-get="/dashboard/analytics" hx-trigger="load"> |
| 69 | Loading analytics... |
| 70 | </div> |
| 71 | |
| 72 | <div hx-get="/dashboard/notifications" hx-trigger="load"> |
| 73 | Loading notifications... |
| 74 | </div> |
| 75 | ``` |
| 76 | |
| 77 | All three requests fire in parallel when the page loads. |
| 78 | |
| 79 | ## Notes |
| 80 | |
| 81 | ### Layout shift |
| 82 | |
| 83 | When the response swaps in, it can push content below it down. This is known as [Cumulative Layout Shift (CLS)](https://web.dev/articles/cls) and affects your [Lighthouse](https://developer.chrome.com/docs/lighthouse) score. |
| 84 | |
| 85 | Reserve space with `min-height`: |
| 86 | |
| 87 | ```html |
| 88 | <div hx-get="/weather" hx-trigger="load" style="min-height: 200px"> |
| 89 | Loading... |
| 90 | </div> |
| 91 | ``` |
| 92 | |
| 93 | The demo above does exactly this. |
| 94 | |
| 95 | ### Infinite loops |
| 96 | |
| 97 | Be careful when including `hx-trigger="load"` in the server response. |
| 98 | |
| 99 | ``` |
| 100 | load → GET /weather → response has hx-trigger="load" → load → GET /weather → … |
| 101 | ``` |
| 102 | |
| 103 | If the response requests the same endpoint, it can create an infinite loop. |