Merge pull request #96 from sbking/use-cookie-fix-document-set-cookie

Fix document.set_cookie usage
This commit is contained in:
Marc-Stefan Cassola 2024-04-01 23:47:56 +01:00 committed by GitHub
commit a984cbef1a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 48 additions and 16 deletions

View file

@ -6,22 +6,33 @@ use rand::prelude::*;
#[component] #[component]
fn Demo() -> impl IntoView { fn Demo() -> impl IntoView {
let (counter, set_counter) = use_cookie::<u32, FromToStringCodec>("counter"); let (counter_a, set_counter_a) = use_cookie::<u32, FromToStringCodec>("counter_a");
let (counter_b, set_counter_b) = use_cookie::<u32, FromToStringCodec>("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() { if counter_a().is_none() {
reset(); reset_a();
}
if counter_b().is_none() {
reset_b();
} }
let increase = move || { let increase_a = move || {
set_counter(counter().map(|c| c + 1)); set_counter_a(counter_a().map(|c| c + 1));
};
let increase_b = move || {
set_counter_b(counter_b().map(|c| c + 1));
}; };
view! { view! {
<p>Counter: {move || counter().map(|c| c.to_string()).unwrap_or("".to_string())}</p> <p>Counter A: {move || counter_a().map(|c| c.to_string()).unwrap_or("".to_string())}</p>
<button on:click=move |_| reset()>Reset</button> <button on:click=move |_| reset_a()>Reset</button>
<button on:click=move |_| increase()>+</button> <button on:click=move |_| increase_a()>+</button>
<p>Counter B: {move || counter_b().map(|c| c.to_string()).unwrap_or("".to_string())}</p>
<button on:click=move |_| reset_b()>Reset</button>
<button on:click=move |_| increase_b()>+</button>
} }
} }

View file

@ -318,8 +318,22 @@ where
} }
} else { } else {
let cookie_name = cookie_name.clone(); let cookie_name = cookie_name.clone();
let ssr_cookies_header_getter = Rc::clone(&ssr_cookies_header_getter);
jar.update_value(|jar| { 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); jar.force_remove(cookie_name);
}); });
@ -692,7 +706,7 @@ fn write_client_cookie(
let document = document(); let document = document();
let document: &web_sys::HtmlDocument = document.unchecked_ref(); 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"))] #[cfg(not(feature = "ssr"))]
@ -718,17 +732,24 @@ fn update_client_cookie_jar(
jar.add_original(cookie); jar.add_original(cookie);
} else { } 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"))] #[cfg(not(feature = "ssr"))]
fn cookie_jar_to_string(jar: &CookieJar) -> String { fn cookie_jar_to_string(jar: &CookieJar, name: &str) -> String {
jar.iter() match jar.get(name) {
.map(|c| c.encoded().to_string()) Some(c) => c.encoded().to_string(),
.collect::<Vec<_>>() None => "".to_string(),
.join("; ") }
} }
fn build_cookie_from_options( fn build_cookie_from_options(