use_cookie signal tries to change cookie headers during SSR

Closes #124
This commit is contained in:
Marc-Stefan Cassola 2024-07-16 20:12:09 +01:00 committed by GitHub
parent 0975bdcfef
commit e192eff406
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -90,7 +90,9 @@ use std::rc::Rc;
/// This works equally well on the server or the client. /// This works equally well on the server or the client.
/// On the server this function reads the cookie from the HTTP request header and writes it back into /// On the server this function reads the cookie from the HTTP request header and writes it back into
/// the HTTP response header according to options (if provided). /// the HTTP response header according to options (if provided).
/// The returned `WriteSignal` will not affect the cookie headers on the server. /// The returned `WriteSignal` may not affect the cookie headers on the server! It will try and write
/// the headers buy if this happens after the headers have already been streamed to the client then
/// this will have no effect.
/// ///
/// > If you're using `axum` you have to enable the `"axum"` feature in your Cargo.toml. /// > If you're using `axum` you have to enable the `"axum"` feature in your Cargo.toml.
/// > In case it's `actix-web` enable the feature `"actix"`, for `spin` enable `"spin"`. /// > In case it's `actix-web` enable the feature `"actix"`, for `spin` enable `"spin"`.
@ -361,29 +363,31 @@ where
#[cfg(feature = "ssr")] #[cfg(feature = "ssr")]
{ {
if !readonly { if !readonly {
let value = cookie create_isomorphic_effect(move |_| {
.with_untracked(|cookie| { let value = cookie
cookie.as_ref().map(|cookie| { .with(|cookie| {
C::encode(cookie) cookie.as_ref().map(|cookie| {
.map_err(|err| on_error(CodecError::Encode(err))) C::encode(cookie)
.ok() .map_err(|err| on_error(CodecError::Encode(err)))
.ok()
})
}) })
}) .flatten();
.flatten(); jar.update_value(|jar| {
jar.update_value(|jar| { write_server_cookie(
write_server_cookie( cookie_name,
cookie_name, value,
value, jar,
jar, max_age,
max_age, expires,
expires, domain,
domain, path,
path, same_site,
same_site, secure,
secure, http_only,
http_only, ssr_set_cookie,
ssr_set_cookie, )
) });
}); });
} }
} }