some more reload optimizations
This commit is contained in:
parent
b102bd754b
commit
662866df5e
6 changed files with 45 additions and 28 deletions
|
|
@ -118,7 +118,9 @@ func OnFileChange(events []*fsnotify.Event) {
|
|||
|
||||
wg.Wait()
|
||||
|
||||
process.KillAll()
|
||||
if tasks.Run {
|
||||
process.KillAll()
|
||||
}
|
||||
|
||||
if tasks.Css {
|
||||
go css.GenerateCss(false)
|
||||
|
|
|
|||
2
framework/assets/dist/htmgo.js
vendored
2
framework/assets/dist/htmgo.js
vendored
File diff suppressed because one or more lines are too long
|
|
@ -10,14 +10,14 @@ htmx.defineExtension("livereload", {
|
|||
createWebSocketClient({
|
||||
url: `ws://${host}/dev/livereload`,
|
||||
onOpen: () => {
|
||||
console.log('LiveReload connected.');
|
||||
},
|
||||
onMessage: (message) => {
|
||||
if(lastVersion === "") {
|
||||
lastVersion = message;
|
||||
}
|
||||
if(lastVersion !== message) {
|
||||
window.location.reload();
|
||||
lastVersion = message;
|
||||
reload()
|
||||
}
|
||||
},
|
||||
onError: (error) => {
|
||||
|
|
@ -30,4 +30,17 @@ htmx.defineExtension("livereload", {
|
|||
onEvent: function (name, evt) {
|
||||
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
function reload() {
|
||||
fetch(window.location.href).then(response => {
|
||||
return response.text();
|
||||
}).then(html => {
|
||||
document.open();
|
||||
document.write(html);
|
||||
document.close();
|
||||
}).catch(err => {
|
||||
console.log('failed to fetch live reload', err)
|
||||
setTimeout(reload, 100)
|
||||
})
|
||||
}
|
||||
|
|
@ -20,11 +20,17 @@ export function createWebSocketClient(opts: WsOpts) {
|
|||
};
|
||||
// Handle connection errors
|
||||
socket.onerror = (error) => {
|
||||
try {
|
||||
socket?.close()
|
||||
} catch(ex) {
|
||||
// noop
|
||||
}
|
||||
let interval = tries * (opts.reconnectInterval || 100);
|
||||
setTimeout(() => connect(tries + 1), interval);
|
||||
};
|
||||
// Handle connection close and attempt reconnection
|
||||
socket.onclose = () => {
|
||||
console.log('WebSocket connection closed. Attempting to reconnect...');
|
||||
let interval = tries * (opts.reconnectInterval || 50);
|
||||
let interval = tries * (opts.reconnectInterval || 100);
|
||||
setTimeout(() => connect(tries + 1), interval);
|
||||
};
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,14 +1,19 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/maddalax/htmgo/framework/h"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
"log"
|
||||
"starter-template/ent"
|
||||
"starter-template/pages"
|
||||
"starter-template/partials/load"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var startTime = time.Now()
|
||||
f := echo.New()
|
||||
|
||||
f.Static("/public", "./assets/dist")
|
||||
|
|
@ -16,16 +21,17 @@ func main() {
|
|||
load.RegisterPartials(f)
|
||||
pages.RegisterPages(f)
|
||||
|
||||
//client, err := ent.Open("sqlite3", "file:ent.db?cache=shared&_fk=1")
|
||||
//if err != nil {
|
||||
// log.Fatalf("failed opening connection to sqlite: %v", err)
|
||||
//}
|
||||
//defer client.Close()
|
||||
//// Run the auto migration tool.
|
||||
//if err := client.Schema.Create(context.Background()); err != nil {
|
||||
// log.Fatalf("failed schema resources: %v", err)
|
||||
//}
|
||||
client, err := ent.Open("sqlite3", "file:ent.db?cache=shared&_fk=1")
|
||||
if err != nil {
|
||||
log.Fatalf("failed opening connection to sqlite: %v", err)
|
||||
}
|
||||
defer client.Close()
|
||||
// Run the auto migration tool.
|
||||
if err := client.Schema.Create(context.Background()); err != nil {
|
||||
log.Fatalf("failed schema resources: %v", err)
|
||||
}
|
||||
|
||||
log.Printf("main() ready in %s", time.Since(startTime))
|
||||
h.Start(f, h.App{
|
||||
LiveReload: true,
|
||||
})
|
||||
|
|
|
|||
|
|
@ -17,18 +17,8 @@ func IndexPage(c echo.Context) *h.Page {
|
|||
),
|
||||
h.Body(
|
||||
h.Class("flex flex-col gap-4"),
|
||||
h.Div(
|
||||
h.Class("flex flex-col items-center justify-center gap-6 p-12 text-center"),
|
||||
h.H1(
|
||||
h.Class("text-4xl sm:text-5xl font-bold max-w-3xl"),
|
||||
h.Text("changed this"),
|
||||
),
|
||||
h.P(
|
||||
h.Class("text-lg sm:text-xl max-w-1xl"),
|
||||
),
|
||||
h.Div(
|
||||
Button(),
|
||||
),
|
||||
h.Div(h.Class("flex flex-col gap-2 mt-6"),
|
||||
Button(),
|
||||
),
|
||||
),
|
||||
))
|
||||
|
|
|
|||
Loading…
Reference in a new issue