Skip to content

Commit

Permalink
register server_fn first to allow for wildcard Route path (#2435)
Browse files Browse the repository at this point in the history
It's normal to have a `NotFound` page with a wildcard path like this
```
<Routes>
    ...
    <Route path="*any" view=NotFound>
</Routes>
```
In `ssr` mode, most servers do a `first match win` approach, so we
should register server functions before view routes, or else a wildcard
route would block all api requests.

https://discord.com/channels/1031524867910148188/1218508054442545185

Signed-off-by: 司芳源 <[email protected]>
  • Loading branch information
sify21 authored Mar 19, 2024
1 parent ac75999 commit 104c09f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 38 deletions.
32 changes: 18 additions & 14 deletions integrations/actix/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1210,6 +1210,15 @@ where
IV: IntoView + 'static,
{
let mut router = self;

// register server functions first to allow for wildcard route in Leptos's Router
for (path, _) in server_fn::actix::server_fn_paths() {
let additional_context = additional_context.clone();
let handler = handle_server_fns_with_context(additional_context);
router = router.route(path, handler);
}

// register routes defined in Leptos's Router
for listing in paths.iter() {
let path = listing.path();
let mode = listing.mode();
Expand Down Expand Up @@ -1272,13 +1281,6 @@ where
}
}

// register server functions
for (path, _) in server_fn::actix::server_fn_paths() {
let additional_context = additional_context.clone();
let handler = handle_server_fns_with_context(additional_context);
router = router.route(path, handler);
}

router
}
}
Expand Down Expand Up @@ -1311,6 +1313,15 @@ impl LeptosRoutes for &mut ServiceConfig {
IV: IntoView + 'static,
{
let mut router = self;

// register server functions first to allow for wildcard route in Leptos's Router
for (path, _) in server_fn::actix::server_fn_paths() {
let additional_context = additional_context.clone();
let handler = handle_server_fns_with_context(additional_context);
router = router.route(path, handler);
}

// register routes defined in Leptos's Router
for listing in paths.iter() {
let path = listing.path();
let mode = listing.mode();
Expand Down Expand Up @@ -1355,13 +1366,6 @@ impl LeptosRoutes for &mut ServiceConfig {
}
}

// register server functions
for (path, _) in server_fn::actix::server_fn_paths() {
let additional_context = additional_context.clone();
let handler = handle_server_fns_with_context(additional_context);
router = router.route(path, handler);
}

router
}
}
Expand Down
48 changes: 24 additions & 24 deletions integrations/axum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1624,6 +1624,30 @@ where

let mut router = self;

// register server functions first to allow for wildcard router path
for (path, method) in server_fn::axum::server_fn_paths() {
let cx_with_state = cx_with_state.clone();
let handler = move |req: Request<Body>| async move {
handle_server_fns_with_context(cx_with_state, req).await
};
router = router.route(
path,
match method {
Method::GET => get(handler),
Method::POST => post(handler),
Method::PUT => put(handler),
Method::DELETE => delete(handler),
Method::PATCH => patch(handler),
_ => {
panic!(
"Unsupported server function HTTP method: \
{method:?}"
);
}
},
);
}

// register router paths
for listing in paths.iter() {
let path = listing.path();
Expand Down Expand Up @@ -1722,30 +1746,6 @@ where
}
}

// register server functions
for (path, method) in server_fn::axum::server_fn_paths() {
let cx_with_state = cx_with_state.clone();
let handler = move |req: Request<Body>| async move {
handle_server_fns_with_context(cx_with_state, req).await
};
router = router.route(
path,
match method {
Method::GET => get(handler),
Method::POST => post(handler),
Method::PUT => put(handler),
Method::DELETE => delete(handler),
Method::PATCH => patch(handler),
_ => {
panic!(
"Unsupported server function HTTP method: \
{method:?}"
);
}
},
);
}

router
}

Expand Down

0 comments on commit 104c09f

Please sign in to comment.