fix: unregister server functions whose paths are in excluded routes (closes #2735) (#3138)

This commit is contained in:
Greg Johnston 2024-10-21 09:14:36 -04:00 committed by GitHub
parent 7b4c470155
commit 7904e0c395
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 37 additions and 2 deletions

View file

@ -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},
@ -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

View file

@ -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};
@ -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

View file

@ -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();
@ -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,