Skip to content

Commit 7bf3348

Browse files
committed
Rename server-watch to dev and serve assets from disk in dev mode
Renames the live-reload makefile target to `make dev` for the standard dev-server convention. Adds a runtime fallback in `server.rs`: when `LEPTOS_OUTPUT_NAME` is set (i.e. the binary was spawned by `cargo leptos watch`), assets are read from `target/site/pkg/` on disk instead of the embedded bytes baked into the binary at compile time. This makes `make dev` work — previously the bin saw stale (or empty) embedded assets because `build.rs` runs before cargo-leptos has produced fresh frontend artifacts. Production binaries built via `make install` still use embedded assets unchanged.
1 parent 3de6d30 commit 7bf3348

3 files changed

Lines changed: 40 additions & 12 deletions

File tree

‎CLAUDE.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ make test-cli # cargo test --test cli_snapshots
3636
make update-snapshots # cargo insta review (after intentional output changes)
3737
make format # cargo clippy --fix && cargo fmt
3838
make server-build # cargo leptos build (produces target/site/pkg/*)
39-
make server-watch # live-reload server against examples/journal.yaml
39+
make dev # live-reload server against examples/journal.yaml
4040
make wasm-build # builds webapp/pkg/ for the browser demo
4141
make docs # builds docs/ via mdbook + the browser demo
4242
make install # server-build + cargo install --path .

‎makefile‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ server-build:
4343
cargo leptos build
4444

4545

46-
.PHONY: server-watch
47-
server-watch:
46+
.PHONY: dev
47+
dev:
4848
cargo leptos watch -- server examples/journal.yaml
4949

5050

‎src/server.rs‎

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use axum::Router;
33
use leptos::prelude::*;
44
use leptos_axum::handle_server_fns_with_context;
55
use std::net::SocketAddr;
6+
use std::path::PathBuf;
67

78
use crate::Ledger;
89

@@ -28,25 +29,49 @@ const SHELL_HTML: &str = r#"<!DOCTYPE html>
2829
<body></body>
2930
</html>"#;
3031

32+
fn dev_mode() -> bool {
33+
std::env::var("LEPTOS_OUTPUT_NAME").is_ok()
34+
}
35+
36+
fn disk_asset_path(name: &str) -> PathBuf {
37+
let root = std::env::var("LEPTOS_SITE_ROOT")
38+
.unwrap_or_else(|_| "target/site".to_string());
39+
let pkg_dir =
40+
std::env::var("LEPTOS_SITE_PKG_DIR").unwrap_or_else(|_| "pkg".to_string());
41+
PathBuf::from(root).join(pkg_dir).join(name)
42+
}
43+
44+
fn load_asset(name: &str, embedded: &'static [u8]) -> Vec<u8> {
45+
if dev_mode() {
46+
if let Ok(bytes) = std::fs::read(disk_asset_path(name)) {
47+
return bytes;
48+
}
49+
}
50+
embedded.to_vec()
51+
}
52+
3153
async fn serve_shell() -> impl IntoResponse {
3254
Html(SHELL_HTML)
3355
}
3456

3557
async fn serve_js() -> impl IntoResponse {
3658
(
3759
[("content-type", "application/javascript")],
38-
embedded_assets::JS,
60+
load_asset("transity.js", embedded_assets::JS),
3961
)
4062
}
4163

4264
async fn serve_css() -> impl IntoResponse {
43-
([("content-type", "text/css")], embedded_assets::CSS)
65+
(
66+
[("content-type", "text/css")],
67+
load_asset("transity.css", embedded_assets::CSS),
68+
)
4469
}
4570

4671
async fn serve_wasm() -> impl IntoResponse {
4772
(
4873
[("content-type", "application/wasm")],
49-
embedded_assets::WASM,
74+
load_asset("transity.wasm", embedded_assets::WASM),
5075
)
5176
}
5277

@@ -55,13 +80,16 @@ async fn serve_favicon() -> impl IntoResponse {
5580
}
5681

5782
pub async fn start(ledger: Ledger, port: u16) -> anyhow::Result<()> {
58-
if !embedded_assets::ASSETS_AVAILABLE {
83+
let disk_assets_available = dev_mode()
84+
&& ["transity.js", "transity.css", "transity.wasm"]
85+
.iter()
86+
.all(|n| disk_asset_path(n).exists());
87+
88+
if !embedded_assets::ASSETS_AVAILABLE && !disk_assets_available {
5989
return Err(anyhow::anyhow!(
60-
"Frontend assets are not embedded in this binary.\n\
61-
The frontend was not built before this binary was compiled.\n\
62-
Rebuild with:\n\
63-
\n cargo leptos build && cargo install --path .\n\n\
64-
(or `make server-build && make install`)"
90+
"Frontend assets are not available.\n\
91+
For development, run `make dev`.\n\
92+
For installation, run `make server-build && make install`."
6593
));
6694
}
6795

0 commit comments

Comments
 (0)