fix loading livereload extension

This commit is contained in:
maddalax 2024-11-09 12:32:30 -06:00 committed by rafaelDev0ps
parent 030d24cda6
commit 4570bf7cf3
4 changed files with 20 additions and 17 deletions

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,13 @@
export function hasExtension(name: string): boolean {
for (const element of Array.from(document.querySelectorAll("[hx-ext]"))) {
const value = element.getAttribute("hx-ext");
if(value != null) {
const split = value.split(" ").map(s => s.replace(",", ""))
if(split.includes(name)) {
return true;
}
}
}
return false;
}

View file

@ -1,24 +1,18 @@
import htmx from "htmx.org"; import htmx from "htmx.org";
import {hasExtension} from "./extension";
let lastVersion = ""; let lastVersion = "";
htmx.defineExtension("livereload", { htmx.defineExtension("livereload", {
init: function () { init: function () {
let enabled = false let enabled = hasExtension("livereload")
for (const element of Array.from(htmx.findAll("[hx-ext]"))) {
const value = element.getAttribute("hx-ext");
if(value?.split(" ").includes("livereload")) {
enabled = true
break;
}
}
if(!enabled) { if(!enabled) {
return return
} }
console.log('livereload extension initialized.'); console.info('livereload extension initialized.');
// Create a new EventSource object and point it to your SSE endpoint // Create a new EventSource object and point it to your SSE endpoint
const eventSource = new EventSource('/dev/livereload'); const eventSource = new EventSource('/dev/livereload');
// Listen for messages from the server // Listen for messages from the server

View file

@ -1,13 +1,9 @@
import {ws} from "./ws"; import {ws} from "./ws";
import {hasExtension} from "./extension";
window.onload = function () { window.onload = function () {
const elements = document.querySelectorAll("[hx-extension]"); if(hasExtension("ws")) {
for (let element of Array.from(elements)) {
const value = element.getAttribute("hx-extension");
if(value != null && value.split(" ").includes("ws")) {
addWsEventHandlers() addWsEventHandlers()
break;
}
} }
}; };