| 1 | (()=>{ |
| 2 | let api; |
| 3 | |
| 4 | function initializePreload(elt) { |
| 5 | let preloadSpec = api.attributeValue(elt, "hx-preload"); |
| 6 | if (preloadSpec == undefined && !elt._htmx?.boosted) return; |
| 7 | |
| 8 | let preloadEvents = [] |
| 9 | let timeout = 5000; |
| 10 | if (preloadSpec) { |
| 11 | let specs = api.parseTriggerSpecs(preloadSpec); |
| 12 | if (specs.length === 0) return; |
| 13 | for (const spec of specs) { |
| 14 | preloadEvents.push(spec.name) |
| 15 | if (spec.timeout) { |
| 16 | timeout = htmx.parseInterval(spec.timeout) |
| 17 | } |
| 18 | } |
| 19 | } else { |
| 20 | let isBoostedAnchor = elt._htmx?.boosted && elt.tagName === "A"; |
| 21 | let isHxGet = api.attributeValue(elt, "hx-get") != null; |
| 22 | if (!isBoostedAnchor && !isHxGet) return; |
| 23 | if (isBoostedAnchor && htmx.config?.preload?.autoBoost === false) return; |
| 24 | if (htmx.config?.preload?.boostTimeout) { |
| 25 | timeout = htmx.parseInterval(htmx.config.preload.boostTimeout) |
| 26 | } |
| 27 | preloadEvents.push(htmx.config?.preload?.boostEvent || "mousedown"); |
| 28 | preloadEvents.push("touchstart"); |
| 29 | } |
| 30 | |
| 31 | let preloadListener = async (evt) => { |
| 32 | let {method} = api.determineMethodAndAction(elt, evt); |
| 33 | if (method !== 'GET') return; |
| 34 | |
| 35 | if (elt._htmx?.preload) return; |
| 36 | |
| 37 | let ctx = api.createRequestContext(elt, evt); |
| 38 | let form = elt.form || elt.closest("form"); |
| 39 | let body = api.collectFormData(elt, form, evt.submitter); |
| 40 | let valsResult = api.getAttributeObject(elt, 'hx-vals', obj => { |
| 41 | for (let key in obj) body.set(key, obj[key]); |
| 42 | }); |
| 43 | if (valsResult) await valsResult; |
| 44 | |
| 45 | let action = ctx.request.action.replace?.(/#.*$/, ''); |
| 46 | |
| 47 | let params = new URLSearchParams(body); |
| 48 | if (params.size) action += (/\?/.test(action) ? "&" : "?") + params; |
| 49 | |
| 50 | elt._htmx.preload = { |
| 51 | prefetch: fetch(action, ctx.request), |
| 52 | action: action, |
| 53 | expiresAt: Date.now() + timeout |
| 54 | }; |
| 55 | |
| 56 | try { |
| 57 | await elt._htmx.preload.prefetch; |
| 58 | } catch (error) { |
| 59 | delete elt._htmx.preload; |
| 60 | } |
| 61 | }; |
| 62 | for (let eventName of preloadEvents) { |
| 63 | elt.addEventListener(eventName, preloadListener, { passive: true }); |
| 64 | } |
| 65 | elt._htmx.preloadListener = preloadListener; |
| 66 | elt._htmx.preloadEvents = preloadEvents; |
| 67 | } |
| 68 | |
| 69 | htmx.registerExtension('preload', { |
| 70 | init: (internalAPI) => { |
| 71 | api = internalAPI; |
| 72 | }, |
| 73 | |
| 74 | htmx_after_init: (elt) => { |
| 75 | initializePreload(elt); |
| 76 | }, |
| 77 | |
| 78 | htmx_before_request: (elt, detail) => { |
| 79 | let {ctx} = detail; |
| 80 | if (elt._htmx?.preload && |
| 81 | elt._htmx.preload.action === ctx.request.action && |
| 82 | Date.now() < elt._htmx.preload.expiresAt) { |
| 83 | let prefetch = elt._htmx.preload.prefetch; |
| 84 | ctx.fetch = () => prefetch; |
| 85 | delete elt._htmx.preload; |
| 86 | } else { |
| 87 | if (elt._htmx) delete elt._htmx.preload; |
| 88 | } |
| 89 | }, |
| 90 | |
| 91 | htmx_before_cleanup: (elt) => { |
| 92 | if (elt._htmx?.preloadListener) { |
| 93 | for (let eventName of elt._htmx.preloadEvents) { |
| 94 | elt.removeEventListener(eventName, elt._htmx.preloadListener); |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | }); |
| 99 | })(); |