diff --git a/examples/project/Cargo.toml b/examples/project/Cargo.toml index 8083698c..4d39d952 100644 --- a/examples/project/Cargo.toml +++ b/examples/project/Cargo.toml @@ -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", ] @@ -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 diff --git a/examples/workspace/Cargo.toml b/examples/workspace/Cargo.toml index 94b5faa3..26cfbbe4 100644 --- a/examples/workspace/Cargo.toml +++ b/examples/workspace/Cargo.toml @@ -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 diff --git a/src/compile/tests.rs b/src/compile/tests.rs index 7ad8cc60..8e0f53cd 100644 --- a/src/compile/tests.rs +++ b/src/compile/tests.rs @@ -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"); @@ -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 \ @@ -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(); diff --git a/src/config/dotenvs.rs b/src/config/dotenvs.rs index d59629ce..11005192 100644 --- a/src/config/dotenvs.rs +++ b/src/config/dotenvs.rs @@ -55,6 +55,8 @@ fn overlay(conf: &mut ProjectConfig, envs: impl Iterator 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 => {} diff --git a/src/config/project.rs b/src/config/project.rs index 96ce3420..fd88efdd 100644 --- a/src/config/project.rs +++ b/src/config/project.rs @@ -47,6 +47,8 @@ pub struct Project { pub hash_file: HashFile, pub hash_files: bool, pub js_minify: bool, + pub server_fn_prefix: Option, + pub disable_server_fn_hash: bool, } impl Debug for Project { @@ -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() } } @@ -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)); } @@ -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 } @@ -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, + + /// 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)] diff --git a/src/config/snapshots/cargo_leptos__config__tests__workspace.snap b/src/config/snapshots/cargo_leptos__config__tests__workspace.snap index 283fdc1a..df7cb6b9 100644 --- a/src/config/snapshots/cargo_leptos__config__tests__workspace.snap +++ b/src/config/snapshots/cargo_leptos__config__tests__workspace.snap @@ -1,6 +1,7 @@ --- source: src/config/tests.rs expression: conf +snapshot_kind: text --- Config { projects: [ @@ -71,6 +72,10 @@ Config { dir: "project1/assets", }, ), + server_fn_prefix: Some( + "/custom/prefix", + ), + disable_server_fn_hash: true, .. }, Project { @@ -144,6 +149,8 @@ Config { dir: "project2/src/assets", }, ), + server_fn_prefix: None, + disable_server_fn_hash: false, .. }, ], diff --git a/src/config/snapshots/cargo_leptos__config__tests__workspace_bin_args_project2.snap b/src/config/snapshots/cargo_leptos__config__tests__workspace_bin_args_project2.snap index 0f04b775..a7adeff8 100644 --- a/src/config/snapshots/cargo_leptos__config__tests__workspace_bin_args_project2.snap +++ b/src/config/snapshots/cargo_leptos__config__tests__workspace_bin_args_project2.snap @@ -1,6 +1,7 @@ --- source: src/config/tests.rs expression: conf +snapshot_kind: text --- Config { projects: [ @@ -80,6 +81,8 @@ Config { dir: "project2/src/assets", }, ), + server_fn_prefix: None, + disable_server_fn_hash: false, .. }, ], diff --git a/src/config/snapshots/cargo_leptos__config__tests__workspace_in_subdir_project2.snap b/src/config/snapshots/cargo_leptos__config__tests__workspace_in_subdir_project2.snap index d51fa27a..db58e922 100644 --- a/src/config/snapshots/cargo_leptos__config__tests__workspace_in_subdir_project2.snap +++ b/src/config/snapshots/cargo_leptos__config__tests__workspace_in_subdir_project2.snap @@ -1,6 +1,7 @@ --- source: src/config/tests.rs expression: conf +snapshot_kind: text --- Config { projects: [ @@ -75,6 +76,8 @@ Config { dir: "project2/src/assets", }, ), + server_fn_prefix: None, + disable_server_fn_hash: false, .. }, ], diff --git a/src/config/snapshots/cargo_leptos__config__tests__workspace_project1.snap b/src/config/snapshots/cargo_leptos__config__tests__workspace_project1.snap index dbd9514f..aa57720f 100644 --- a/src/config/snapshots/cargo_leptos__config__tests__workspace_project1.snap +++ b/src/config/snapshots/cargo_leptos__config__tests__workspace_project1.snap @@ -1,6 +1,7 @@ --- source: src/config/tests.rs expression: conf +snapshot_kind: text --- Config { projects: [ @@ -71,6 +72,10 @@ Config { dir: "project1/assets", }, ), + server_fn_prefix: Some( + "/custom/prefix", + ), + disable_server_fn_hash: true, .. }, ], diff --git a/src/config/snapshots/cargo_leptos__config__tests__workspace_project2.snap b/src/config/snapshots/cargo_leptos__config__tests__workspace_project2.snap index 2f374121..4d66e0a1 100644 --- a/src/config/snapshots/cargo_leptos__config__tests__workspace_project2.snap +++ b/src/config/snapshots/cargo_leptos__config__tests__workspace_project2.snap @@ -1,6 +1,7 @@ --- source: src/config/tests.rs expression: conf +snapshot_kind: text --- Config { projects: [ @@ -75,6 +76,8 @@ Config { dir: "project2/src/assets", }, ), + server_fn_prefix: None, + disable_server_fn_hash: false, .. }, ],