head-support 扩展在响应 htmx 请求时添加了对 head 标签 的支持。
¥The head-support extension adds support for head tags
in responses to htmx requests.
htmx 最初是一个专注于部分替换 body 标记内的 HTML 的库。因此,合并诸如 head 标签之类的附加信息并不是该库的重点。(这与 TurboLinks 等形成对比,后者专注于将通过 AJAX 检索的整个网页合并到浏览器中。)
¥htmx began as a library focused on partial replacement of HTML within the body tag. As such, merging additional
information such as the head tag was not a focus of the library. (This is in contrast with, for example, TurboLinks,
which was focused on merging entire web pages retrieved via AJAX into the browser.)
hx-boost 属性使 htmx 更接近完全 HTML 文档支持的世界,并且最终添加了从 head 元素中提取 title 标签的支持,但完全 head 标签支持从未成为库的功能。此扩展解决了该缺点。
¥The hx-boost attribute moved htmx closer to this world of full HTML-document support &
support for extracting the title tag out of head elements was eventually added, but full head tag support has never been
a feature of the library. This extension addresses that shortcoming.
¥Installing
安装 head-support 的最快方法是通过 CDN 加载它。请记住始终在扩展和 启用扩展 之前包含核心 htmx 库。
¥The fastest way to install head-support is to load it via a CDN. Remember to always include the core htmx library before the extension and enable the extension.
<head>
<script src="https://cdn.jsdelivr.net/npm/htmx.org@2.0.5/dist/htmx.min.js" integrity="sha384-HGfztofotfshcF7+8n44JQL2oJmowVChPTg48S+jvZoztPfvwD79OC/LTtG6dMp+" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/htmx-ext-head-support@2.0.2" integrity="sha384-cvMqHzjCJsOHgGuyB3sWXaUSv/Krm0BdzjuI1rtkjCbL1l1oHJx+cHyVRJhyuEz0" crossorigin="anonymous"></script>
</head>
<body hx-ext="head-support">
...
https://cdn.jsdelivr.net/npm/htmx-ext-head-support/dist/head-support.js 上还有一个未缩小的版本。
¥An unminified version is also available at https://cdn.jsdelivr.net/npm/htmx-ext-head-support/dist/head-support.js.
虽然 CDN 方法很简单,但你可能需要考虑 在生产中不使用 CDN。安装 head-support 的下一个最简单的方法是将其复制到你的项目中。从 https://cdn.jsdelivr.net/npm/htmx-ext-head-support 下载扩展,将其添加到项目中的相应目录中,并在必要时使用 <script> 标签将其包含在内。
¥While the CDN approach is simple, you may want to consider not using CDNs in production. The next easiest way to install head-support is to simply copy it into your project. Download the extension from https://cdn.jsdelivr.net/npm/htmx-ext-head-support, add it to the appropriate directory in your project and include it where necessary with a <script> tag.
对于 npm 样式的构建系统,你可以通过 npm 安装 head-support:
¥For npm-style build systems, you can install head-support via npm:
npm install htmx-ext-head-support
安装后,你需要使用适当的工具来打包 node_modules/htmx-ext-head-support/dist/head-support.js(或 .min.js)。例如,你可以将扩展与来自 node_modules/htmx.org/dist/htmx.js 的 htmx 核心和项目特定代码打包在一起。
¥After installing, you’ll need to use appropriate tooling to bundle node_modules/htmx-ext-head-support/dist/head-support.js (or .min.js). For example, you might bundle the extension with htmx core from node_modules/htmx.org/dist/htmx.js and project-specific code.
如果你使用打包器来管理你的 javascript(例如 Webpack、Rollup):
¥If you are using a bundler to manage your javascript (e.g. Webpack, Rollup):
通过 npm 安装 htmx.org 和 htmx-ext-head-support
¥Install htmx.org and htmx-ext-head-support via npm
将两个包导入到你的 index.js
¥Import both packages to your index.js
import `htmx.org`;
import `htmx-ext-head-support`;
¥Usage
<body hx-ext="head-support">
...
</body>
安装此功能后,htmx 收到的所有包含 head 标签的响应(即使它们不是具有根 <html> 元素的完整 HTML 文档)都将被处理。
¥With this installed, 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.
head 标签的处理方式取决于 htmx 请求的类型。
¥How the head tag is handled depends on the type of htmx request.
如果 htmx 请求来自增强元素,则使用以下合并算法:
¥If the htmx request is from a boosted element, then the following merge algorithm is used:
当前头部中作为精确文本匹配存在的元素将保留在原处
¥Elements that exist in the current head as exact textual matches will be left in place
当前头部中不存在的元素将添加到 head 标签的末尾
¥Elements that do not exist in the current head will be added at the end of the head tag
当前头部中存在但不在新头部中的元素将从头部中删除
¥Elements that exist in the current head, but not in the new head will be removed from the head
如果 htmx 请求来自非增强元素,则所有内容将附加到现有的 head 元素。
¥If the htmx request is from a non-boosted element, then all content will be appended to the existing head element.
如果你希望在任一情况下覆盖此行为,则可以将 hx-head 属性放在新的 <head> 标签上,并使用以下两个值之一:
¥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:
merge - 遵循上面概述的合并算法
¥merge - follow the merging algorithm outlined above
append - 将元素附加到现有头部
¥append - append the elements to the existing head
¥Controlling Merge Behavior
除此之外,你还可以使用以下属性控制各个元素的合并行为:
¥Beyond this, you may also control merging behavior of individual elements with the following attributes:
如果你将 hx-head="re-eval" 放在 head 元素上,它将在每个请求中重新添加(删除和附加)到 head 标记,即使它已经存在。例如,这对于在每个 htmx 请求上执行脚本很有用。
¥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.
如果你将 hx-preserve="true" 放在元素上,它将永远不会从 head 中删除
¥If you place hx-preserve="true" on an element, it will never be removed from the head
¥Example
例如,请考虑现有文档中的以下 head 标签:
¥As an example, consider the following head tag in an existing document:
<head>
<link rel="stylesheet" href="https://the.missing.style">
<link rel="stylesheet" href="/css/site1.css">
<script src="/js/script1.js"></script>
<script src="/js/script2.js"></script>
</head>
如果 htmx 收到包含此新 head 标签的请求:
¥If htmx receives a request containing this new head tag:
<head>
<link rel="stylesheet" href="https://the.missing.style">
<link rel="stylesheet" href="/css/site2.css">
<script src="/js/script2.js"></script>
<script src="/js/script3.js"></script>
</head>
然后将发生以下操作:
¥Then the following operations will occur:
<link rel="stylesheet" href="https://the.missing.style"> 将保持不变
¥<link rel="stylesheet" href="https://the.missing.style"> will be left alone
<link rel="stylesheet" href="/css/site1.css"> 将从头部移除
¥<link rel="stylesheet" href="/css/site1.css"> will be removed from the head
<link rel="stylesheet" href="/css/site2.css"> 将被添加到头部
¥<link rel="stylesheet" href="/css/site2.css"> will be added to the head
<script src="/js/script1.js"></script> 将从头部移除
¥<script src="/js/script1.js"></script> will be removed from the head
<script src="/js/script2.js"></script> 将保持不变
¥<script src="/js/script2.js"></script> will be left alone
<script src="/js/script3.js"></script> 将被添加到头部
¥<script src="/js/script3.js"></script> will be added to the head
最终的 head 元素将如下所示:
¥The final head element will look like this:
<head>
<link rel="stylesheet" href="https://the.missing.style">
<script src="/js/script2.js"></script>
<link rel="stylesheet" href="/css/site2.css">
<script src="/js/script3.js"></script>
</head>
¥Events
此扩展触发以下事件:
¥This extension triggers the following events:
htmx:removingHeadElement - 即将删除 head 元素时触发,删除的元素在 event.detail.headElement 中可用。如果在事件上调用 preventDefault(),则不会删除元素。
¥htmx:removingHeadElement - triggered when a head element is about to be removed, with the element being removed
available in event.detail.headElement. If preventDefault() is invoked on the event, the element will not be removed.
htmx:addingHeadElement - 即将添加 head 元素时触发,添加的元素在 event.detail.headElement 中可用。如果在事件上调用 preventDefault(),则不会添加元素。
¥htmx:addingHeadElement - triggered when a head element is about to be added, with the element being added
available in event.detail.headElement. If preventDefault() is invoked on the event, the element will not be added.
htmx:afterHeadMerge - 在发生 head 标签合并后触发,事件 detail 中可用的值如下:
¥htmx:afterHeadMerge - triggered after a head tag merge has occurred, with the following values available in the event detail:
added - 添加的 head 元素
¥added - added head elements
kept - 保留 head 元素
¥kept - kept head elements
removed - 删除 head 元素
¥removed - removed head elements
htmx:beforeHeadMerge - 在发生 head 合并之前触发
¥htmx:beforeHeadMerge - triggered before a head merge occurs