Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow disabling server fn hash and customizing the default prefix #3438

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

spencewenski
Copy link
Contributor

@spencewenski spencewenski commented Jan 3, 2025

Allow configuring the default prefix for server function API routes. 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.

Also, allow disabling 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.

I also opened a cargo-leptos PR to add support for setting the env vars based on values in the leptos config: leptos-rs/cargo-leptos#412

Allow configuring the default prefix for server function API routes. 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.

Also, allow disabling 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.
spencewenski added a commit to spencewenski/cargo-leptos that referenced this pull request Jan 3, 2025
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
spencewenski added a commit to spencewenski/cargo-leptos that referenced this pull request Jan 3, 2025
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
@@ -78,6 +78,12 @@ pub struct LeptosOptions {
#[builder(default = default_hash_files())]
#[serde(default = "default_hash_files")]
pub hash_files: bool,
#[builder(default, setter(strip_option))]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LeptosOptions should be marked as non-exhaustive so fields can be added without requiring a breaking semver update. However, adding a non-exhaustive annotation is also a breaking change. I’m happy to add it if you want, and if there’s another version bump coming up I can switch this PR to that branch if one exists.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd probably be good to add, the release of 0.8 is likely soonish

@benwis
Copy link
Contributor

benwis commented Jan 6, 2025

Overall I think this looks good and would be a useful change. The hashes were added by default primarily to prevent collisions between server fn paths. Have you experimented to see what happens in that case? If I recall correctly it should throw an error.

Otherwise I think this is good to go

@spencewenski
Copy link
Contributor Author

The hashes were added by default primarily to prevent collisions between server fn paths. Have you experimented to see what happens in that case?

Yeah, Axum throws an error when you try to add a duplicate route. I can add a comment calling this out. I think it’s okay though since removing the hashes is an opt-in behavior.

I was thinking another option to prevent conflicts could be to use the module path as the prefix for the api route. From what I was seeing though that’s not possible in macros, but I’m not super familiar with macros so I could be wrong.

@benwis
Copy link
Contributor

benwis commented Jan 7, 2025

The hashes were added by default primarily to prevent collisions between server fn paths. Have you experimented to see what happens in that case?

Yeah, Axum throws an error when you try to add a duplicate route. I can add a comment calling this out. I think it’s okay though since removing the hashes is an opt-in behavior.

I was thinking another option to prevent conflicts could be to use the module path as the prefix for the api route. From what I was seeing though that’s not possible in macros, but I’m not super familiar with macros so I could be wrong.

A comment's good, overall I don't think we have to worry about additional conflict detection as long as it throws a proper error

@spencewenski
Copy link
Contributor Author

Yeah, Axum throws an error when you try to add a duplicate route.

I was double checking this, and I was correct that Axum throws an error. However, it appears that Actix doesn't. In that case, do we want to add a check somewhere in Leptos?

@spencewenski
Copy link
Contributor Author

spencewenski commented Jan 7, 2025

I was thinking another option to prevent conflicts could be to use the module path as the prefix for the api route. From what I was seeing though that’s not possible in macros, but I’m not super familiar with macros so I could be wrong.

I actually figured out how to do this. It would require adding const-str as a dependency in server_fn, similar to how const_format is added as a dependency. I think I would be interested in this for my project, so I'll open this as a separate PR.

Here's the code: spencewenski#1. I'll move the PR to the main leptos repo once this PR is merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants