serialize data in the server and deserialize data in the client

This commit is contained in:
Evan Almloff committed 2024-02-02 17:23:00 -06:00
1 parent 8b489db5ac
commit 1c4e1d84ea
5 files changed
+26 -23

No files matched your search

@@ -26,7 +26,7 @@ pub(crate) fn serde_from_bytes<T: DeserializeOwned>(string: &[u8]) -> Option<T>
static SERVER_DATA: once_cell::sync::Lazy<Option<HTMLDataCursor>> =
once_cell::sync::Lazy::new(|| {
#[cfg(target_arch = "wasm32")]
#[cfg(all(feature = "web", target_arch = "wasm32"))]
{
let window = web_sys::window()?.document()?;
let element = match window.get_element_by_id("dioxus-storage-data") {
@@ -48,7 +48,7 @@ static SERVER_DATA: once_cell::sync::Lazy<Option<HTMLDataCursor>> =
Some(data.cursor())
}
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(all(feature = "web", target_arch = "wasm32")))]
{
None
}
@@ -63,11 +63,7 @@ pub(crate) fn take_server_data<T: DeserializeOwned>() -> Option<T> {
///
/// When dioxus-fullstack renders the page, it will serialize the root props and put them in the document. This function gets them from the document.
pub fn get_root_props_from_document<T: DeserializeOwned>() -> Option<T> {
#[cfg(not(target_arch = "wasm32"))]
{
None
}
#[cfg(target_arch = "wasm32")]
#[cfg(all(feature = "web", target_arch = "wasm32"))]
{
let attribute = web_sys::window()?
.document()?
@@ -76,4 +72,8 @@ pub fn get_root_props_from_document<T: DeserializeOwned>() -> Option<T> {
serde_from_bytes(attribute.as_bytes())
}
#[cfg(not(all(feature = "web", target_arch = "wasm32")))]
{
None
}
}