| 1 | --- |
| 2 | title: "hx-preload" |
| 3 | description: "Preload content on hover for instant page loads" |
| 4 | category: "Performance" |
| 5 | icon: "icon-[mdi--rocket-launch-outline]" |
| 6 | keywords: ["preload", "prefetch", "performance", "hover", "mousedown"] |
| 7 | --- |
| 8 | |
| 9 | The `preload` extension allows you to load HTML fragments into your browser's cache before they are requested by the user, so that additional pages appear to load nearly instantaneously. |
| 10 | |
| 11 | **Important:** Preloading content judiciously can improve your web application's perceived performance, but preloading too many resources can negatively impact your visitors' bandwidth and your server performance. Use this extension carefully! |
| 12 | |
| 13 | ## Installing |
| 14 | |
| 15 | ```html |
| 16 | <script src="https://cdn.jsdelivr.net/npm/htmx.org@__VERSION__/dist/htmx.min.js"></script> |
| 17 | <script src="https://cdn.jsdelivr.net/npm/htmx.org@__VERSION__/dist/ext/hx-preload.js"></script> |
| 18 | ``` |
| 19 | |
| 20 | ## Usage |
| 21 | |
| 22 | Add an `hx-preload` attribute to any boosted hyperlinks and [`hx-get`](/reference/attributes/hx-get) elements you want to preload. By default, resources will be loaded as soon as the `mousedown` event begins, giving your application a roughly 100-200ms head start on serving responses. |
| 23 | |
| 24 | ```html |
| 25 | <button hx-get="/server/1" hx-preload>Preloaded on mousedown</button> |
| 26 | <button hx-get="/server/2" hx-preload="mouseover">Preloaded on mouseover</button> |
| 27 | ``` |
| 28 | |
| 29 | All preload requests include an additional `HX-Preloaded: true` header. |
| 30 | |
| 31 | ## hx-boost Integration |
| 32 | |
| 33 | When the preload extension is loaded, all [`hx-boost`](/reference/attributes/hx-boost) anchor tags are automatically preloaded on `mousedown` without needing an explicit `hx-preload` attribute. To opt out of this behaviour, set `htmx.config.preload.autoBoost = false`. |
| 34 | |
| 35 | ```html |
| 36 | <!-- these are all automatically preloaded when the preload extension is loaded --> |
| 37 | <nav hx-boost="true"> |
| 38 | <a href="/page1">Page 1</a> |
| 39 | <a href="/page2">Page 2</a> |
| 40 | </nav> |
| 41 | ``` |
| 42 | |
| 43 | To disable auto-preloading of boosted links: |
| 44 | |
| 45 | ```html |
| 46 | <meta name="htmx-config" content='preload:{"autoBoost":false}'> |
| 47 | ``` |
| 48 | |
| 49 | Plain `<a href>` links without `hx-boost` cannot be preloaded, as preloading works by warming an htmx request — it does not intercept browser navigation. |
| 50 | |
| 51 | ## Trigger Events |
| 52 | |
| 53 | ### `hx-preload` / `hx-preload="mousedown"` (default) |
| 54 | |
| 55 | Begins loading when the user presses the mouse down. Conservative — guarantees the user intends to click. Gives your server a 100-200ms head start. |
| 56 | |
| 57 | ### `hx-preload="mouseover"` |
| 58 | |
| 59 | Preloads when the user's mouse hovers over the element. More aggressive — gives your server several hundred milliseconds of head start. |
| 60 | |
| 61 | ### `hx-preload="custom-event-name"` |
| 62 | |
| 63 | Preload can listen to any event name supported by htmx trigger specs. |
| 64 | |
| 65 | ```html |
| 66 | <button hx-get="/data" hx-preload="focus">Preload on focus</button> |
| 67 | ``` |
| 68 | |
| 69 | ## Configuration |
| 70 | |
| 71 | All options are set via `htmx.config.preload`: |
| 72 | |
| 73 | | Option | Default | Description | |
| 74 | |--------|---------|-------------| |
| 75 | | `autoBoost` | `true` | Automatically preload all `hx-boost` anchor tags on `mousedown` | |
| 76 | | `boostEvent` | `"mousedown"` | The event used to trigger preloading for auto-boosted links | |
| 77 | | `boostTimeout` | `5000` | Milliseconds before a preloaded response for a boosted link expires | |
| 78 | |
| 79 | ```html |
| 80 | <meta name="htmx-config" content='preload:{"autoBoost":true,"boostEvent":"mouseover","boostTimeout":"3s"}'> |
| 81 | ``` |
| 82 | |
| 83 | ## Timeout |
| 84 | |
| 85 | Preloaded responses expire after 5 seconds by default. Use the `timeout` modifier to override per element: |
| 86 | |
| 87 | ```html |
| 88 | <button hx-get="/data" hx-preload="mousedown timeout:2s">Expires after 2s</button> |
| 89 | ``` |
| 90 | |
| 91 | ## Limitations |
| 92 | |
| 93 | - Only `GET` requests can be preloaded. `POST`, `PUT`, `PATCH` and `DELETE` will not be preloaded. |
| 94 | - Plain `<a href>` links without `hx-boost` or `hx-get` cannot be preloaded. |
| 95 | - Preloaded responses will only be reused if the actual request is made before the timeout expires. |
| 96 | |
| 97 | ## Upgrading from htmx 2.x |
| 98 | |
| 99 | - The `preload` attribute is now named `hx-preload`. |
| 100 | - Inherited preload via a parent element now requires the `:inherited` modifier: `hx-preload:inherited`. |