Skip to content

Commit

Permalink
feat: Add server function configs
Browse files Browse the repository at this point in the history
Add support to configure and set the `SERVER_FN_PREFIX` and
`DISABLE_SERVER_FN_HASH` env vars using the leptos project config.

See the following PR for more info: leptos-rs/leptos#3438
  • Loading branch information
spencewenski committed Jan 3, 2025
1 parent f03212f commit 30833d8
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 21 deletions.
37 changes: 20 additions & 17 deletions examples/project/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,25 @@ opt-level = 'z'
[features]
default = ["ssr"]
hydrate = [
"leptos/hydrate",
"leptos_meta/hydrate",
"leptos_router/hydrate",
"dep:wasm-bindgen",
"dep:console_log",
"dep:console_error_panic_hook",
"leptos/hydrate",
"leptos_meta/hydrate",
"leptos_router/hydrate",
"dep:wasm-bindgen",
"dep:console_log",
"dep:console_error_panic_hook",
]
ssr = [
"leptos/ssr",
"leptos_meta/ssr",
"leptos_router/ssr",
"dep:leptos_actix",
"dep:reqwest",
"dep:actix-web",
"dep:actix-files",
"dep:futures",
"dep:simple_logger",
"dep:serde_json",
"dep:dotenvy",
"leptos/ssr",
"leptos_meta/ssr",
"leptos_router/ssr",
"dep:leptos_actix",
"dep:reqwest",
"dep:actix-web",
"dep:actix-files",
"dep:futures",
"dep:simple_logger",
"dep:serde_json",
"dep:dotenvy",
]


Expand Down Expand Up @@ -122,3 +122,6 @@ env = "dev"
#
# Optional: Defaults to false. Can also be set with the LEPTOS_HASH_FILES=false env var
hash-files = true

server-fn-prefix = "/custom/prefix"
disable-server-fn-hash = true
2 changes: 2 additions & 0 deletions examples/workspace/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ lib-package = "front-package"
assets-dir = "project1/assets"
style-file = "project1/css/main.scss"
site-root = "target/site/project1"
server-fn-prefix = "/custom/prefix"
disable-server-fn-hash = true
12 changes: 9 additions & 3 deletions src/compile/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ fn test_project_dev() {
LEPTOS_JS_MINIFY=false \
LEPTOS_HASH_FILES=true \
LEPTOS_HASH_FILE_NAME=hash.txt \
LEPTOS_WATCH=true";
LEPTOS_WATCH=true \
SERVER_FN_PREFIX=/custom/prefix \
DISABLE_SERVER_FN_HASH=true";
assert_eq!(ENV_REF, envs);

assert_snapshot!(cargo, @"cargo build --package=example --bin=example --no-default-features --features=ssr");
Expand Down Expand Up @@ -107,7 +109,9 @@ fn test_workspace_project1() {
LEPTOS_BIN_DIR=project1\\server \
LEPTOS_JS_MINIFY=false \
LEPTOS_HASH_FILES=false \
LEPTOS_WATCH=true"
LEPTOS_WATCH=true \
SERVER_FN_PREFIX=/custom/prefix \
DISABLE_SERVER_FN_HASH=true"
} else {
"\
LEPTOS_OUTPUT_NAME=project1 \
Expand All @@ -119,7 +123,9 @@ fn test_workspace_project1() {
LEPTOS_BIN_DIR=project1/server \
LEPTOS_JS_MINIFY=false \
LEPTOS_HASH_FILES=false \
LEPTOS_WATCH=true"
LEPTOS_WATCH=true \
SERVER_FN_PREFIX=/custom/prefix \
DISABLE_SERVER_FN_HASH=true"
};

let cli = dev_opts();
Expand Down
2 changes: 2 additions & 0 deletions src/config/dotenvs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ fn overlay(conf: &mut ProjectConfig, envs: impl Iterator<Item = (String, String)
"LEPTOS_BIN_TARGET_DIR" => conf.bin_target_dir = Some(val),
"LEPTOS_BIN_CARGO_COMMAND" => conf.bin_cargo_command = Some(val),
"LEPTOS_JS_MINIFY" => conf.js_minify = val.parse()?,
"SERVER_FN_PREFIX" => conf.server_fn_prefix = Some(val),
"DISABLE_SERVER_FN_HASH" => conf.disable_server_fn_hash = true,
// put these here to suppress the warning, but there's no
// good way at the moment to pull the ProjectConfig all the way to Exe
exe::ENV_VAR_LEPTOS_TAILWIND_VERSION => {}
Expand Down
34 changes: 33 additions & 1 deletion src/config/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ pub struct Project {
pub hash_file: HashFile,
pub hash_files: bool,
pub js_minify: bool,
pub server_fn_prefix: Option<String>,
pub disable_server_fn_hash: bool,
}

impl Debug for Project {
Expand All @@ -64,6 +66,8 @@ impl Debug for Project {
.field("site", &self.site)
.field("end2end", &self.end2end)
.field("assets", &self.assets)
.field("server_fn_prefix", &self.server_fn_prefix)
.field("disable_server_fn_hash", &self.disable_server_fn_hash)
.finish_non_exhaustive()
}
}
Expand Down Expand Up @@ -126,6 +130,8 @@ impl Project {
hash_file,
hash_files: config.hash_files,
js_minify: cli.release && cli.js_minify && config.js_minify,
server_fn_prefix: config.server_fn_prefix,
disable_server_fn_hash: config.disable_server_fn_hash,
};
resolved.push(Arc::new(proj));
}
Expand Down Expand Up @@ -159,7 +165,13 @@ impl Project {
vec.push(("LEPTOS_HASH_FILE_NAME", self.hash_file.rel.to_string()));
}
if self.watch {
vec.push(("LEPTOS_WATCH", "true".to_string()))
vec.push(("LEPTOS_WATCH", true.to_string()))
}
if let Some(prefix) = self.server_fn_prefix.as_ref() {
vec.push(("SERVER_FN_PREFIX", prefix.clone()));
}
if self.disable_server_fn_hash {
vec.push(("DISABLE_SERVER_FN_HASH", true.to_string()));
}
vec
}
Expand Down Expand Up @@ -226,6 +238,26 @@ pub struct ProjectConfig {
#[serde(default)]
pub bin_default_features: bool,

/// The default prefix to use for server functions when generating API routes. Can be
/// overridden for individual functions using `#[server(prefix = "...")]` as usual.
///
/// This is useful to override the default prefix (`/api`) for all server functions without
/// needing to manually specify via `#[server(prefix = "...")]` on every server function.
#[serde(default)]
pub server_fn_prefix: Option<String>,

/// Whether to disable appending the server functions' hashes to the end of their API names.
///
/// This is useful when an app's client side needs a stable server API. For example, shipping
/// the CSR WASM binary in a Tauri app. Tauri app releases are dependent on each platform's
/// distribution method (e.g., the Apple App Store or the Google Play Store), which typically
/// are much slower than the frequency at which a website can be updated. In addition, it's
/// common for users to not have the latest app version installed. In these cases, the CSR WASM
/// app would need to be able to continue calling the backend server function API, so the API
/// path needs to be consistent and not have a hash appended.
#[serde(default)]
pub disable_server_fn_hash: bool,

#[serde(skip)]
pub config_dir: Utf8PathBuf,
#[serde(skip)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
source: src/config/tests.rs
expression: conf
snapshot_kind: text
---
Config {
projects: [
Expand Down Expand Up @@ -71,6 +72,10 @@ Config {
dir: "project1/assets",
},
),
server_fn_prefix: Some(
"/custom/prefix",
),
disable_server_fn_hash: true,
..
},
Project {
Expand Down Expand Up @@ -144,6 +149,8 @@ Config {
dir: "project2/src/assets",
},
),
server_fn_prefix: None,
disable_server_fn_hash: false,
..
},
],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
source: src/config/tests.rs
expression: conf
snapshot_kind: text
---
Config {
projects: [
Expand Down Expand Up @@ -80,6 +81,8 @@ Config {
dir: "project2/src/assets",
},
),
server_fn_prefix: None,
disable_server_fn_hash: false,
..
},
],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
source: src/config/tests.rs
expression: conf
snapshot_kind: text
---
Config {
projects: [
Expand Down Expand Up @@ -75,6 +76,8 @@ Config {
dir: "project2/src/assets",
},
),
server_fn_prefix: None,
disable_server_fn_hash: false,
..
},
],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
source: src/config/tests.rs
expression: conf
snapshot_kind: text
---
Config {
projects: [
Expand Down Expand Up @@ -71,6 +72,10 @@ Config {
dir: "project1/assets",
},
),
server_fn_prefix: Some(
"/custom/prefix",
),
disable_server_fn_hash: true,
..
},
],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
source: src/config/tests.rs
expression: conf
snapshot_kind: text
---
Config {
projects: [
Expand Down Expand Up @@ -75,6 +76,8 @@ Config {
dir: "project2/src/assets",
},
),
server_fn_prefix: None,
disable_server_fn_hash: false,
..
},
],
Expand Down

0 comments on commit 30833d8

Please sign in to comment.