-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
made hot_reloading controlled by env and refactored
- Loading branch information
Showing
3 changed files
with
62 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,17 @@ | ||
import envoy | ||
import gleam/string_builder.{type StringBuilder} | ||
import nakai | ||
import nakai/html | ||
import nakai/html/attrs | ||
import wisp | ||
|
||
pub fn layout(page: html.Node(a)) { | ||
let hot_reloading_scripts = case envoy.get("DEVELOPMENT") { | ||
Ok("0") | Ok("false") | Ok("FALSE") -> [html.Nothing] | ||
Ok(_) -> hot_reloading() | ||
Error(_) -> [html.Nothing] | ||
} | ||
|
||
let layout = | ||
html.Html([], [ | ||
html.Doctype("html"), | ||
|
@@ -17,50 +24,9 @@ pub fn layout(page: html.Node(a)) { | |
html.title("HTMX Gleam!"), | ||
]), | ||
html.Body([], [ | ||
html.div( | ||
[ | ||
attrs.id("reload"), | ||
attrs.Attr("hx-get", "/reload"), | ||
attrs.Attr("hx-trigger", "every 0.5s"), | ||
attrs.Attr("hx-target", "#hot-reload"), | ||
], | ||
[ | ||
html.div([attrs.id("hot-reload")], [ | ||
html.Text("This will reload every second"), | ||
]), | ||
], | ||
), | ||
html.h1([], [html.Text("Hello, World!")]), | ||
page, | ||
html.Script( | ||
" | ||
let serverStopped = false; | ||
let reload = undefined; | ||
document.body.addEventListener('htmx:sendError', function(evt) { | ||
console.log(evt) | ||
const target = evt.detail.target; | ||
if (target.id === 'hot-reload') { | ||
serverStopped = true; | ||
} | ||
}); | ||
document.body.addEventListener('htmx:afterRequest', function(evt) { | ||
const target = evt.detail.target; | ||
if (target.id === 'hot-reload') { | ||
if (evt.detail.failed) { | ||
serverStopped = true; | ||
} | ||
if (evt.detail.successful) { | ||
if (serverStopped) { | ||
console.log('Server restarted!'); | ||
serverStopped = false; | ||
window.location.reload(); | ||
} | ||
} | ||
} | ||
}); | ||
", | ||
), | ||
..hot_reloading_scripts | ||
]), | ||
]) | ||
|
||
|
@@ -89,9 +55,55 @@ pub fn render_partial(partial: html.Node(a)) { | |
} | ||
|
||
fn inject_script_src(html: StringBuilder) -> StringBuilder { | ||
string_builder.replace( | ||
html, | ||
html | ||
|> string_builder.replace( | ||
"</head>", | ||
"<script src='https://unpkg.com/[email protected]'></script></head>", | ||
) | ||
} | ||
|
||
fn hot_reloading() -> List(html.Node(n)) { | ||
[ | ||
html.div( | ||
[ | ||
attrs.id("reload"), | ||
attrs.Attr("hx-get", "/reload"), | ||
attrs.Attr("hx-trigger", "every 0.5s"), | ||
attrs.Attr("hx-target", "#hot-reload"), | ||
], | ||
[ | ||
html.div([attrs.id("hot-reload")], [ | ||
html.Text("This will reload every second"), | ||
]), | ||
], | ||
), | ||
html.Script( | ||
" | ||
let serverStopped = false; | ||
let reload = undefined; | ||
document.body.addEventListener('htmx:sendError', function(evt) { | ||
console.log(evt) | ||
const target = evt.detail.target; | ||
if (target.id === 'hot-reload') { | ||
serverStopped = true; | ||
} | ||
}); | ||
document.body.addEventListener('htmx:afterRequest', function(evt) { | ||
const target = evt.detail.target; | ||
if (target.id === 'hot-reload') { | ||
if (evt.detail.failed) { | ||
serverStopped = true; | ||
} | ||
if (evt.detail.successful) { | ||
if (serverStopped) { | ||
console.log('Server restarted!'); | ||
serverStopped = false; | ||
window.location.reload(); | ||
} | ||
} | ||
} | ||
}); | ||
", | ||
), | ||
] | ||
} |