From e2c84523f201ffe3cc5f44f2b6229e963e0635a8 Mon Sep 17 00:00:00 2001 From: Stephen Brian King <3913213+sbking@users.noreply.github.com> Date: Fri, 22 Mar 2024 23:23:38 -0600 Subject: [PATCH 1/2] fix document.set_cookie usage --- src/use_cookie.rs | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/src/use_cookie.rs b/src/use_cookie.rs index af634a2..9d06a55 100644 --- a/src/use_cookie.rs +++ b/src/use_cookie.rs @@ -318,8 +318,22 @@ where } } else { let cookie_name = cookie_name.clone(); + let ssr_cookies_header_getter = Rc::clone(&ssr_cookies_header_getter); jar.update_value(|jar| { + update_client_cookie_jar( + &cookie_name, + &None, + jar, + max_age, + expires, + &domain, + &path, + same_site, + secure, + http_only, + ssr_cookies_header_getter, + ); jar.force_remove(cookie_name); }); @@ -692,7 +706,7 @@ fn write_client_cookie( let document = document(); let document: &web_sys::HtmlDocument = document.unchecked_ref(); - document.set_cookie(&cookie_jar_to_string(jar)).ok(); + document.set_cookie(&cookie_jar_to_string(jar, name)).ok(); } #[cfg(not(feature = "ssr"))] @@ -718,17 +732,24 @@ fn update_client_cookie_jar( jar.add_original(cookie); } else { - jar.force_remove(name); + let max_age = Some(0); + let expires = Some(0); + let value = ""; + let cookie = build_cookie_from_options( + name, max_age, expires, http_only, secure, path, same_site, domain, value, + ); + + jar.add(cookie); } } } #[cfg(not(feature = "ssr"))] -fn cookie_jar_to_string(jar: &CookieJar) -> String { - jar.iter() - .map(|c| c.encoded().to_string()) - .collect::>() - .join("; ") +fn cookie_jar_to_string(jar: &CookieJar, name: &str) -> String { + match jar.get(name) { + Some(c) => c.encoded().to_string(), + None => "".to_string(), + } } fn build_cookie_from_options( From 705fbf97ac032167068e42b48f7a1376d9bcd2d0 Mon Sep 17 00:00:00 2001 From: Stephen Brian King <3913213+sbking@users.noreply.github.com> Date: Fri, 22 Mar 2024 23:35:59 -0600 Subject: [PATCH 2/2] add a second counter to use_cookie example --- examples/use_cookie/src/main.rs | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/examples/use_cookie/src/main.rs b/examples/use_cookie/src/main.rs index 094509d..0429b28 100644 --- a/examples/use_cookie/src/main.rs +++ b/examples/use_cookie/src/main.rs @@ -6,22 +6,33 @@ use rand::prelude::*; #[component] fn Demo() -> impl IntoView { - let (counter, set_counter) = use_cookie::("counter"); + let (counter_a, set_counter_a) = use_cookie::("counter_a"); + let (counter_b, set_counter_b) = use_cookie::("counter_b"); - let reset = move || set_counter(Some(random())); + let reset_a = move || set_counter_a(Some(random())); + let reset_b = move || set_counter_b(Some(random())); - if counter().is_none() { - reset(); + if counter_a().is_none() { + reset_a(); + } + if counter_b().is_none() { + reset_b(); } - let increase = move || { - set_counter(counter().map(|c| c + 1)); + let increase_a = move || { + set_counter_a(counter_a().map(|c| c + 1)); + }; + let increase_b = move || { + set_counter_b(counter_b().map(|c| c + 1)); }; view! { -

Counter: {move || counter().map(|c| c.to_string()).unwrap_or("—".to_string())}

- - +

Counter A: {move || counter_a().map(|c| c.to_string()).unwrap_or("—".to_string())}

+ + +

Counter B: {move || counter_b().map(|c| c.to_string()).unwrap_or("—".to_string())}

+ + } }