From 8b59cb93836bfd13b9d38a245c64374baa489722 Mon Sep 17 00:00:00 2001 From: Carson Gross Date: Fri, 5 Jan 2024 13:11:30 -0700 Subject: [PATCH] docs + config of head handling --- src/htmx.js | 36 ++++++++++++----------- test/index.html | 2 +- www/content/attributes/hx-swap.md | 9 ++++++ www/content/docs.md | 48 ++++++++++++++++++++++++++++--- 4 files changed, 74 insertions(+), 21 deletions(-) diff --git a/src/htmx.js b/src/htmx.js index 4ecd9298..9794df4c 100644 --- a/src/htmx.js +++ b/src/htmx.js @@ -61,7 +61,7 @@ var htmx = (function() { disableInheritance: false, head : { boost : "merge", - other: "title", + other: "none", }, responseHandling: [ { code: '204', swap: false }, @@ -1143,17 +1143,12 @@ var htmx = (function() { } } - function handleHeadTag(head, defaultStrategy) { - - if (head && htmx.config.head) { - - if (defaultStrategy === "none") { - return - } + function handleHeadTag(head, strategy) { + if (head && (strategy === "merge" || strategy === "append")) { // allow new head to override merge strategy - let elementMergeStrategy = getAttributeValue(head, "hx-head") || defaultStrategy; - if (elementMergeStrategy === "append" || elementMergeStrategy === "merge") { + let elementMergeStrategy = getAttributeValue(head, "hx-head") || strategy; + if (elementMergeStrategy === "merge" || elementMergeStrategy === "append") { let removed = [] let appended = [] @@ -2216,7 +2211,7 @@ var htmx = (function() { const historyElement = getHistoryElement() const settleInfo = makeSettleInfo(historyElement) handleTitle(fragment.title); - handleHeadTag(fragment.head, "merge"); + handleHeadTag(fragment.head, htmx.config.head.boost); // @ts-ignore swapInnerHTML(historyElement, content, settleInfo) @@ -2239,7 +2234,7 @@ var htmx = (function() { const historyElement = getHistoryElement() const settleInfo = makeSettleInfo(historyElement) handleTitle(fragment.title); - handleHeadTag(fragment.head, "merge"); + handleHeadTag(fragment.head, htmx.config.head.boost); swapInnerHTML(historyElement, fragment, settleInfo) settleImmediately(settleInfo.tasks); setTimeout(function() { @@ -2586,6 +2581,8 @@ var htmx = (function() { swapSpec.transition = value.substr(11) === 'true' } else if (value.indexOf('ignoreTitle:') === 0) { swapSpec.ignoreTitle = value.substr(12) === 'true' + } else if (value.indexOf('head:') === 0) { + swapSpec.head = value.substr(5) } else if (value.indexOf('scroll:') === 0) { const scrollSpec = value.substr(7) var splitSpec = scrollSpec.split(':') @@ -3381,6 +3378,7 @@ var htmx = (function() { const shouldSwap = responseHandling.swap let isError = !!responseHandling.error let ignoreTitle = htmx.config.ignoreTitle || responseHandling.ignoreTitle + let head = responseInfo.boosted ? htmx.config.head.boost : htmx.config.head.other let selectOverride = responseHandling.select if (responseHandling.target) { responseInfo.target = querySelectorExt(elt, responseHandling.target) @@ -3408,7 +3406,8 @@ var htmx = (function() { serverResponse, isError, ignoreTitle, - selectOverride + selectOverride, + head }, responseInfo) if (responseHandling.event && !triggerEvent(target, responseHandling.event, beforeSwapDetails)) return @@ -3419,6 +3418,7 @@ var htmx = (function() { serverResponse = beforeSwapDetails.serverResponse // allow updating content isError = beforeSwapDetails.isError // allow updating error ignoreTitle = beforeSwapDetails.ignoreTitle // allow updating ignoring title + head = beforeSwapDetails.head // allow updating head algorithm selectOverride = beforeSwapDetails.selectOverride // allow updating select override responseInfo.target = target // Make updated target available to response events @@ -3447,6 +3447,9 @@ var htmx = (function() { if (swapSpec.hasOwnProperty('ignoreTitle')) { ignoreTitle = swapSpec.ignoreTitle } + if (swapSpec.hasOwnProperty('head')) { + head = swapSpec.head + } target.classList.add(htmx.config.swappingClass) @@ -3524,10 +3527,10 @@ var htmx = (function() { handleTitle(settleInfo.title); } + console.log("Here", head) // merge in new head after swap but before settle if (triggerEvent(document.body, "htmx:beforeHeadMerge", {head: settleInfo.head})) { - handleHeadTag(settleInfo.head, responseInfo.boosted ? htmx.config.head.boost : - htmx.config.head.other); + handleHeadTag(settleInfo.head, head); } if (hasHeader(xhr, /HX-Trigger-After-Swap:/i)) { @@ -3642,8 +3645,9 @@ var htmx = (function() { * @param {import("./htmx").HtmxExtension} extension */ function defineExtension(name, extension) { + if (name === "head-support") return; // ignore the head support extension, now integrated into htmx if (extension.init) { - extension.init(internalAPI) + extension.init(internalAPI); } extensions[name] = mergeObjects(extensionBase(), extension) } diff --git a/test/index.html b/test/index.html index d512a2ea..6722c52f 100644 --- a/test/index.html +++ b/test/index.html @@ -9,7 +9,7 @@ - + diff --git a/www/content/attributes/hx-swap.md b/www/content/attributes/hx-swap.md index f4ef401a..c0a04b6c 100644 --- a/www/content/attributes/hx-swap.md +++ b/www/content/attributes/hx-swap.md @@ -39,6 +39,15 @@ If you want to use the new [View Transitions](https://developer.mozilla.org/en-U when a swap occurs, you can use the `transition:true` option for your swap. You can also enable this feature globally by setting the `htmx.config.globalViewTransitions` config setting to `true`. +#### `head` tag handling: `head` + +If you want to modify how a `head` tag found in the new content is handled, you can use the `head` modifier, with one +of the following values: + +* `merge` - merge the new head tag elements into the existing element +* `append` - append the new head tag elements to the existing head tag +* `none` - ignore any new head tag elements + #### Timing: `swap` & `settle` You can modify the amount of time that htmx will wait after receiving a response to swap the content diff --git a/www/content/docs.md b/www/content/docs.md index bad4ddc0..e14a6cda 100644 --- a/www/content/docs.md +++ b/www/content/docs.md @@ -29,6 +29,7 @@ custom_classes = "wide-content" * [confirming](#confirming) * [inheritance](#inheritance) * [boosting](#boosting) + * [head tag handling](#head) * [websockets & SSE](#websockets-and-sse) * [history](#history) * [requests & responses](#requests) @@ -182,10 +183,6 @@ To upgrade to htmx 2.0 from htmx 1.0, you will need to do the following: `htmx.config.methodsThatUseUrlParams` to `["get"]` (it's a little crazy, but `DELETE`, according to the spec, should use request parameters.) * If you want to make cross-domain requests with htmx, revert `htmx.config.selfRequestsOnly` to `false` - * If you want to revert these to the htmx 1.x defaults, you can use the following meta tag: - ```html - - ``` * Convert any `hx-on` attributes to their `hx-on:` equivalent: ```html