Skip to content

Commit

Permalink
fix: unregister server functions whose paths are in excluded routes (c…
Browse files Browse the repository at this point in the history
…loses #2735) (#3138)
  • Loading branch information
gbj authored Oct 21, 2024
1 parent 7b4c470 commit 7904e0c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
8 changes: 7 additions & 1 deletion integrations/actix/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ use once_cell::sync::Lazy;
use parking_lot::RwLock;
use send_wrapper::SendWrapper;
use server_fn::{
redirect::REDIRECT_HEADER, request::actix::ActixRequest, ServerFnError,
actix::unregister_server_fns, redirect::REDIRECT_HEADER,
request::actix::ActixRequest, ServerFnError,
};
use std::{
fmt::{Debug, Display},
Expand Down Expand Up @@ -1004,6 +1005,11 @@ where
{
let _ = any_spawner::Executor::init_tokio();

// remove any server fns that match excluded paths
if let Some(excluded) = &excluded_routes {
unregister_server_fns(excluded);
}

let owner = Owner::new_root(Some(Arc::new(SsrSharedContext::new())));
let (mock_meta, _) = ServerMetaContext::new();
let routes = owner
Expand Down
9 changes: 8 additions & 1 deletion integrations/axum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ use leptos_router::{
#[cfg(feature = "default")]
use once_cell::sync::Lazy;
use parking_lot::RwLock;
use server_fn::{redirect::REDIRECT_HEADER, ServerFnError};
use server_fn::{
axum::unregister_server_fns, redirect::REDIRECT_HEADER, ServerFnError,
};
#[cfg(feature = "default")]
use std::path::Path;
use std::{fmt::Debug, io, pin::Pin, sync::Arc};
Expand Down Expand Up @@ -1338,6 +1340,11 @@ where
init_executor();
let owner = Owner::new_root(Some(Arc::new(SsrSharedContext::new())));

// remove any server fns that match excluded paths
if let Some(excluded) = &excluded_routes {
unregister_server_fns(excluded);
}

let routes = owner
.with(|| {
// stub out a path for now
Expand Down
22 changes: 22 additions & 0 deletions server_fn/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,17 @@ pub mod axum {
.map(|item| (item.path(), item.method()))
}

/// Removes any server functions with an included path from the map of
/// registered server functions.
///
/// Calling this will mean that these server functions are not found unless you provide
/// alternate handlers for them in your application.
pub fn unregister_server_fns(paths: &[String]) {
if !paths.is_empty() {
REGISTERED_SERVER_FUNCTIONS.retain(|(p, _), _| !paths.contains(p));
}
}

/// An Axum handler that responds to a server function request.
pub async fn handle_server_fn(req: Request<Body>) -> Response<Body> {
let path = req.uri().path();
Expand Down Expand Up @@ -588,6 +599,17 @@ pub mod actix {
.map(|item| (item.path(), item.method()))
}

/// Removes any server functions with an included path from the map of
/// registered server functions.
///
/// Calling this will mean that these server functions are not found unless you provide
/// alternate handlers for them in your application.
pub fn unregister_server_fns(paths: &[String]) {
if !paths.is_empty() {
REGISTERED_SERVER_FUNCTIONS.retain(|(p, _), _| !paths.contains(p));
}
}

/// An Actix handler that responds to a server function request.
pub async fn handle_server_fn(
req: HttpRequest,
Expand Down

0 comments on commit 7904e0c

Please sign in to comment.