Skip to content

Commit e6140b4

Browse files
committed
fix: include shared merge and deferred props in the inertia page object
1 parent 9ec9fba commit e6140b4

File tree

2 files changed

+28
-18
lines changed

2 files changed

+28
-18
lines changed

src/providers/actix_provider/impls.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,17 @@ impl InertiaResponder<HttpResponse, HttpRequest, Redirect> for Inertia {
5151
&'b self,
5252
req: &'b HttpRequest,
5353
component: Component,
54-
props: InertiaProps<'b>,
54+
mut props: InertiaProps<'b>,
5555
) -> Result<HttpResponse, InertiaError> {
56+
let shared_props = req
57+
.extensions_mut()
58+
.remove::<SharedProps>()
59+
.map(|shared_props| shared_props.0);
60+
61+
if let Some(shared_props) = shared_props {
62+
props.extend(shared_props);
63+
}
64+
5665
let url = req.uri().to_string();
5766
let req_type: InertiaRequestType = req.get_request_type()?;
5867

@@ -63,17 +72,7 @@ impl InertiaResponder<HttpResponse, HttpRequest, Redirect> for Inertia {
6372
let reset = req.get_merge_props_to_be_reset();
6473
let deferred_props = get_deferred_props(&props, &req_type);
6574
let merge_props = get_mergeable_props(&props, reset);
66-
let mut props = resolve_props(&props, &req_type).await?;
67-
68-
let shared_props = req
69-
.extensions()
70-
.get::<SharedProps>()
71-
.map(|shared_props| shared_props.0.clone());
72-
73-
if let Some(shared_props) = shared_props {
74-
let shared_props = resolve_props(&shared_props, &req_type).await?;
75-
props.extend(shared_props);
76-
}
75+
let props = resolve_props(&props, &req_type).await?;
7776

7877
let page = InertiaPage::new(
7978
component,

tests/test_p_actix.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -435,16 +435,16 @@ async fn foo(_baz: &HttpRequest) {}
435435

436436
#[tokio::test]
437437
async fn test_shared_props() {
438-
const TEST_SHARED_PROPERTY_KEY: &str = "sharedProperty";
439-
const TEST_SHARED_PROPERTY_VALUE: &str = "Some amazing value!";
440-
441438
let app = actix_web::test::init_service(generate_actix_app().await.wrap(
442439
InertiaMiddleware::new().with_shared_props(Arc::new(move |req| {
443440
let req = req.clone();
444441
async move {
445442
foo(&req).await;
446443
hashmap![
447-
TEST_SHARED_PROPERTY_KEY => InertiaProp::always(TEST_SHARED_PROPERTY_VALUE),
444+
"sharedProp" => InertiaProp::always("Some amazing value!"),
445+
"deferredSharedProp" => InertiaProp::defer(prop_resolver!({
446+
vec!["foo", "bar", "baz"].into_inertia_value()
447+
}))
448448
]
449449
}
450450
.boxed_local()
@@ -468,8 +468,19 @@ async fn test_shared_props() {
468468
let json_body: InertiaPage = serde_json::from_slice(&body[..]).unwrap();
469469

470470
assert_eq!(
471-
TEST_SHARED_PROPERTY_VALUE,
472-
json_body.get_props().get(TEST_SHARED_PROPERTY_KEY).unwrap()
471+
"Some amazing value!",
472+
json_body.get_props().get("sharedProp").unwrap()
473+
);
474+
475+
println!("{:#?}", json_body);
476+
assert_eq!(
477+
&vec!["deferredSharedProp"],
478+
json_body
479+
.get_deferred_props()
480+
.as_ref()
481+
.unwrap()
482+
.get("default")
483+
.unwrap()
473484
);
474485
}
475486

0 commit comments

Comments
 (0)