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()
|
wg.Wait()
|
||||||
|
|
||||||
process.KillAll()
|
if tasks.Run {
|
||||||
|
process.KillAll()
|
||||||
|
}
|
||||||
|
|
||||||
if tasks.Css {
|
if tasks.Css {
|
||||||
go css.GenerateCss(false)
|
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({
|
createWebSocketClient({
|
||||||
url: `ws://${host}/dev/livereload`,
|
url: `ws://${host}/dev/livereload`,
|
||||||
onOpen: () => {
|
onOpen: () => {
|
||||||
console.log('LiveReload connected.');
|
|
||||||
},
|
},
|
||||||
onMessage: (message) => {
|
onMessage: (message) => {
|
||||||
if(lastVersion === "") {
|
if(lastVersion === "") {
|
||||||
lastVersion = message;
|
lastVersion = message;
|
||||||
}
|
}
|
||||||
if(lastVersion !== message) {
|
if(lastVersion !== message) {
|
||||||
window.location.reload();
|
lastVersion = message;
|
||||||
|
reload()
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
onError: (error) => {
|
onError: (error) => {
|
||||||
|
|
@ -30,4 +30,17 @@ htmx.defineExtension("livereload", {
|
||||||
onEvent: function (name, evt) {
|
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
|
// Handle connection errors
|
||||||
socket.onerror = (error) => {
|
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
|
// Handle connection close and attempt reconnection
|
||||||
socket.onclose = () => {
|
socket.onclose = () => {
|
||||||
console.log('WebSocket connection closed. Attempting to reconnect...');
|
let interval = tries * (opts.reconnectInterval || 100);
|
||||||
let interval = tries * (opts.reconnectInterval || 50);
|
|
||||||
setTimeout(() => connect(tries + 1), interval);
|
setTimeout(() => connect(tries + 1), interval);
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,19 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
"github.com/maddalax/htmgo/framework/h"
|
"github.com/maddalax/htmgo/framework/h"
|
||||||
_ "github.com/mattn/go-sqlite3"
|
_ "github.com/mattn/go-sqlite3"
|
||||||
|
"log"
|
||||||
|
"starter-template/ent"
|
||||||
"starter-template/pages"
|
"starter-template/pages"
|
||||||
"starter-template/partials/load"
|
"starter-template/partials/load"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
var startTime = time.Now()
|
||||||
f := echo.New()
|
f := echo.New()
|
||||||
|
|
||||||
f.Static("/public", "./assets/dist")
|
f.Static("/public", "./assets/dist")
|
||||||
|
|
@ -16,16 +21,17 @@ func main() {
|
||||||
load.RegisterPartials(f)
|
load.RegisterPartials(f)
|
||||||
pages.RegisterPages(f)
|
pages.RegisterPages(f)
|
||||||
|
|
||||||
//client, err := ent.Open("sqlite3", "file:ent.db?cache=shared&_fk=1")
|
client, err := ent.Open("sqlite3", "file:ent.db?cache=shared&_fk=1")
|
||||||
//if err != nil {
|
if err != nil {
|
||||||
// log.Fatalf("failed opening connection to sqlite: %v", err)
|
log.Fatalf("failed opening connection to sqlite: %v", err)
|
||||||
//}
|
}
|
||||||
//defer client.Close()
|
defer client.Close()
|
||||||
//// Run the auto migration tool.
|
// Run the auto migration tool.
|
||||||
//if err := client.Schema.Create(context.Background()); err != nil {
|
if err := client.Schema.Create(context.Background()); err != nil {
|
||||||
// log.Fatalf("failed schema resources: %v", err)
|
log.Fatalf("failed schema resources: %v", err)
|
||||||
//}
|
}
|
||||||
|
|
||||||
|
log.Printf("main() ready in %s", time.Since(startTime))
|
||||||
h.Start(f, h.App{
|
h.Start(f, h.App{
|
||||||
LiveReload: true,
|
LiveReload: true,
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -17,18 +17,8 @@ func IndexPage(c echo.Context) *h.Page {
|
||||||
),
|
),
|
||||||
h.Body(
|
h.Body(
|
||||||
h.Class("flex flex-col gap-4"),
|
h.Class("flex flex-col gap-4"),
|
||||||
h.Div(
|
h.Div(h.Class("flex flex-col gap-2 mt-6"),
|
||||||
h.Class("flex flex-col items-center justify-center gap-6 p-12 text-center"),
|
Button(),
|
||||||
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(),
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
))
|
))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue