| 1 | --- |
| 2 | title: "hx-head" |
| 3 | description: "Merge head tags (styles, scripts) into htmx responses" |
| 4 | category: "Swap behaviors" |
| 5 | icon: "icon-[mdi--page-layout-header]" |
| 6 | keywords: ["head", "styles", "scripts", "merge", "append"] |
| 7 | --- |
| 8 | |
| 9 | The `head-support` extension adds support for [head tags](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/head) in responses to htmx requests. |
| 10 | |
| 11 | htmx began as a library focused on partial replacement of HTML within the `body` tag. The [`hx-boost`](/reference/attributes/hx-boost) attribute moved htmx closer to full HTML-document support, and support for extracting the `title` tag was eventually added, but full head tag support has never been a feature of the core library. This extension addresses that. |
| 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-head.js"></script> |
| 18 | ``` |
| 19 | |
| 20 | ## Usage |
| 21 | |
| 22 | All responses that htmx receives that contain a `head` tag in them (even if they are not complete HTML documents with a root `<html>` element) will be processed. |
| 23 | |
| 24 | How the head tag is handled depends on the type of htmx request. |
| 25 | |
| 26 | If the htmx request is from a **boosted element**, then the following merge algorithm is used: |
| 27 | |
| 28 | - Elements that exist in the current head as exact textual matches will be left in place |
| 29 | - Elements that do not exist in the current head will be added at the end of the head tag |
| 30 | - Elements that exist in the current head, but not in the new head will be removed from the head |
| 31 | |
| 32 | If the htmx request is from a **non-boosted element**, then all content will be _appended_ to the existing head element. |
| 33 | |
| 34 | If you wish to override this behavior in either case, you can place the `hx-head` attribute on the new `<head>` tag, with either of the following two values: |
| 35 | |
| 36 | - `merge` - follow the merging algorithm outlined above |
| 37 | - `append` - append the elements to the existing head |
| 38 | |
| 39 | ### Controlling Merge Behavior |
| 40 | |
| 41 | You may also control merging behavior of individual elements: |
| 42 | |
| 43 | - If you place `hx-head="re-eval"` on a head element, it will be re-added (removed and appended) to the head tag on every request, even if it already exists. This can be useful to execute a script on every htmx request, for example. |
| 44 | - If you place [`hx-preserve`](/reference/attributes/hx-preserve)`="true"` on an element, it will never be removed from the head. |
| 45 | |
| 46 | ### Example |
| 47 | |
| 48 | Consider the following head tag in an existing document: |
| 49 | |
| 50 | ```html |
| 51 | <head> |
| 52 | <link rel="stylesheet" href="https://the.missing.style"> |
| 53 | <link rel="stylesheet" href="/css/site1.css"> |
| 54 | <script src="/js/script1.js"></script> |
| 55 | <script src="/js/script2.js"></script> |
| 56 | </head> |
| 57 | ``` |
| 58 | |
| 59 | If htmx receives a request containing this new head tag: |
| 60 | |
| 61 | ```html |
| 62 | <head> |
| 63 | <link rel="stylesheet" href="https://the.missing.style"> |
| 64 | <link rel="stylesheet" href="/css/site2.css"> |
| 65 | <script src="/js/script2.js"></script> |
| 66 | <script src="/js/script3.js"></script> |
| 67 | </head> |
| 68 | ``` |
| 69 | |
| 70 | Then the following operations will occur: |
| 71 | |
| 72 | - `<link rel="stylesheet" href="https://the.missing.style">` will be left alone |
| 73 | - `<link rel="stylesheet" href="/css/site1.css">` will be removed from the head |
| 74 | - `<link rel="stylesheet" href="/css/site2.css">` will be added to the head |
| 75 | - `<script src="/js/script1.js"></script>` will be removed from the head |
| 76 | - `<script src="/js/script2.js"></script>` will be left alone |
| 77 | - `<script src="/js/script3.js"></script>` will be added to the head |
| 78 | |
| 79 | ## Events |
| 80 | |
| 81 | - `htmx:removingHeadElement` - triggered when a head element is about to be removed. The element is available in `event.detail.headElement`. Call `preventDefault()` to keep it. |
| 82 | - `htmx:addingHeadElement` - triggered when a head element is about to be added. The element is available in `event.detail.headElement`. Call `preventDefault()` to skip it. |
| 83 | - `htmx:afterHeadMerge` - triggered after a head tag merge has occurred, with `detail.added`, `detail.kept`, and `detail.removed` arrays. |
| 84 | - `htmx:beforeHeadMerge` - triggered before a head merge occurs. |