Suspense boundaries/out of order streaming/anyhow like error handling (#2365)
* create static site generation helpers in the router crate * work on integrating static site generation into fullstack * move ssg into a separate crate * integrate ssg with the launch builder * simplify ssg example * fix static_routes for child routes * move CLI hot reloading websocket code into dioxus-hot-reload * fix some unused imports * use the same hot reloading websocket code for fullstack * fix fullstack hot reloading * move cli hot reloading logic into the hot reload crate * ssg example working with dx serve * add more examples * fix clippy * switch to a result for Element * fix formatting * fix hot reload doctest imports * fix axum imports * add show method to error context * implement retaining nodes during suspense * fix unterminated if statements * communicate between tasks and suspense boundaries * make suspense placeholders easier to use * implement IntoDynNode and IntoVNode for more wrappers * fix clippy examples * fix rsx tests * add streaming html utilities to the ssr package * unify hydration and non-hydration ssr cache * fix router with Result Element * don't run server doc tests * Fix hot reload websocket doc examples * simple apps working with fullstack streaming * fix preloading wasm * Report errors encountered while streaming * remove async from incremental renderer * document new VirtualDom suspense methods * make streaming work with incremental rendering * fix static site generation * document suspense structs * create closure type; allow async event handlers in props; allow shorthand event handlers * test forwarding event handlers with the shorthand syntax * fix clippy * fix imports in spawn async doctest * fix empty rsx * fix async result event handlers * fix mounting router in multiple places * Fix task dead cancel race condition * simplify diffing before adding suspense * fix binary size increase * fix attribute diffing * more diffing fixes * create minimal fullstack feature * smaller fullstack bundles * allow mounting nodes that are already created and creating nodes without mounting them * fix hot reload feature * fix replacing components * don't reclaim virtual nodes * client side suspense working! * fix CLI * slightly smaller fullstack builds * fix multiple suspended scopes * fix merge errors * yield back to tokio every few polls to fix suspending on many tasks at once * remove logs * document suspense boundary and update suspense example * fix ssg * make streaming optional * fix some router and core tests * fix suspense example * fix serialization with out of order server futures * add incremental streaming hackernews demo * fix hackernews demo * fix root hackernews redirect * fix formatting * add tests for suspense cases * slightly smaller binaries * slightly smaller * improve error handling docs * fix errors example link * fix doc tests * remove log file * fix ssr cache type inference * remove index.html * fix ssg render template * fix assigning ids on elements with dynamic attributes * add desktop feature to the workspace examples * remove router static generation example; ssg lives in the dioxus-static-generation package * add a test for effects during suspense * only run effects on mounted nodes * fix multiple suspense roots * fix node iterator * fix closures without arguments * fix dioxus-core readme doctest * remove suspense logs * fix scope stack * fix clippy * remove unused suspense boundary from hackernews * assert that launch never returns for better compiler errors * fix static generation launch function * fix web renderer * pass context providers into server functions * add an example for FromContext * clean up DioxusRouterExt * fix server function context * fix fullstack desktop example * forward CLI serve settings to fullstack * re-export serve config at the root of fullstack * forward env directly instead of using a guard * just set the port in the CLI for fullstack playwright tests * fix fullstack dioxus-cli-config feature * fix launch server merge conflicts * fix fullstack launch context * Merge branch 'main' into suspense-2.0 * fix fullstack html data * remove drop virtual dom feature * add a comment about only_write_templates binary size workaround * remove explicit dependencies from use_server_future * make ErrorContext and SuspenseContext more similar * Tweak: small tweaks to tomls to make diff smaller * only rerun components under suspense after the initial placeholders are sent to the client * add module docs for suspense * keep track of when suspense boundaries are resolved * start implementing JS out of order streaming * fix core tests * implement the server side of suspense with js * fix streaming ssr with nested suspense * move streaming ssr code into fullstack * revert minification changes * serialize server future data as the html streams * start loading scripts wasm immediately instead of defering the script * very basic nested suspense example working with minimal html updates * clean up some suspense/error docs * fix hydrating nested pending server futures * sort resolved boundaries by height * Fix disconnecting clients while streaming * fix static generation crate * don't insert extra divs when hydrating streamed chunks * wait to swap in the elements until they are hydrated * remove inline streaming script * hackernews partially working * fix spa mode * banish the open shadow dom * fix removing placeholder * set up streaming playwright test * run web playwright tests on 9999 to avoid port conflicts with other local servers * remove suspense nodes if the suspense boundary is replaced before the suspense resolves on the server * ignore hydration of removed suspense boundaries * use path based indexing to fix hydrating suspense after parent suspense with child is removed * re-export dioxus error * remove resolved suspense divs if the suspense boundary has been removed * Fix client side initialized server futures * ignore comment nodes while traversing nodes in core to avoid lists getting swapped out with suspense * Pass initial hydration data to the client * hide pre nodes * don't panic if reclaiming an element fails * fix scope stack when polling tasks * improve deserialization out of length message * Ok(VNode::placeholder()) -> VNode::empty() * fix typo in rsx usage * restore testing changes from suspense example * clean up some logs and comments * fix playwright tests * clean up more changes in core * clean up core tests * remove anymap dependency * clean up changes to hooks * clean up changes in the router, rsx, and web * revert changes to axum-hello-world * fix use_server_future * fix clippy in dioxus-core * check that the next or previous node exist before checking if we should ignore them * fix formatting * fix suspense playwright test * remove unused suspense code * add more suspense playwright tests * add more docs for error boundaries * fix suspense core tests * fix ErrorBoundary example * remove a bunch of debug logging in js * fix router failure_external_navigation * use absolute paths in the interpreter build.rs * strip '\r' while hashing ts files * add a wrapper with a default error boundary and suspense boundary * restore hot reloading * ignore non-ts files when hashing * sort ts files before hashing them * fix rsx tests * fix fullstack doc tests * fix core tests * fix axum auth example * update suspense hydration diagram * longer playwright build limit * tiny fixes - spelling, formatting * update diagram link * remove comment and template nodes for suspense placeholders * remove comment nodes as we hydrate text * simplify hackernews example * clean up hydrating text nodes * switch to a separate environment variable for the base path for smaller binaries * clean up file system html trait * fix form data * move streaming code into fullstack * implement serialize and deserialize for CapturedError * remove waits in the nested suspense playwright spec * force sequential fullstack builds for CI * longer nested suspense delay for CI * fix --force-sequential flag * wait to launch server until client build is done --------- Co-authored-by: Jonathan Kelley <jkelleyrtp@gmail.com>
This commit is contained in:
1 parent
ffa36a67c3
commit
022e4ad203
175 files changed
+7872
-4010
No files matched your search
@@ -1,59 +0,0 @@
|
||||
use serde::de::DeserializeOwned;
|
||||
|
||||
use base64::engine::general_purpose::STANDARD;
|
||||
use base64::Engine;
|
||||
|
||||
use super::HTMLDataCursor;
|
||||
|
||||
#[allow(unused)]
|
||||
pub(crate) fn serde_from_bytes<T: DeserializeOwned>(string: &[u8]) -> Option<T> {
|
||||
let decompressed = match STANDARD.decode(string) {
|
||||
Ok(bytes) => bytes,
|
||||
Err(err) => {
|
||||
tracing::error!("Failed to decode base64: {}", err);
|
||||
return None;
|
||||
}
|
||||
};
|
||||
|
||||
match ciborium::from_reader(std::io::Cursor::new(decompressed)) {
|
||||
Ok(data) => Some(data),
|
||||
Err(err) => {
|
||||
tracing::error!("Failed to deserialize: {}", err);
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static SERVER_DATA: once_cell::sync::Lazy<Option<HTMLDataCursor>> =
|
||||
once_cell::sync::Lazy::new(|| {
|
||||
#[cfg(all(feature = "web", target_arch = "wasm32"))]
|
||||
{
|
||||
let window = web_sys::window()?.document()?;
|
||||
let element = match window.get_element_by_id("dioxus-storage-data") {
|
||||
Some(element) => element,
|
||||
None => {
|
||||
tracing::error!("Failed to get element by id: dioxus-storage-data");
|
||||
return None;
|
||||
}
|
||||
};
|
||||
let attribute = match element.get_attribute("data-serialized") {
|
||||
Some(attribute) => attribute,
|
||||
None => {
|
||||
tracing::error!("Failed to get attribute: data-serialized");
|
||||
return None;
|
||||
}
|
||||
};
|
||||
|
||||
let data: super::HTMLData = serde_from_bytes(attribute.as_bytes())?;
|
||||
|
||||
Some(data.cursor())
|
||||
}
|
||||
#[cfg(not(all(feature = "web", target_arch = "wasm32")))]
|
||||
{
|
||||
None
|
||||
}
|
||||
});
|
||||
|
||||
pub(crate) fn take_server_data<T: DeserializeOwned>() -> Option<T> {
|
||||
SERVER_DATA.as_ref()?.take()
|
||||
}
|
||||
@@ -1,112 +1,65 @@
|
||||
#![allow(unused)]
|
||||
use base64::Engine;
|
||||
use std::{io::Cursor, sync::atomic::AtomicUsize};
|
||||
use dioxus_lib::prelude::{has_context, provide_context, use_hook};
|
||||
use serialize::serde_to_writable;
|
||||
use std::{cell::RefCell, io::Cursor, rc::Rc, sync::atomic::AtomicUsize};
|
||||
|
||||
use base64::engine::general_purpose::STANDARD;
|
||||
use serde::{de::DeserializeOwned, Serialize};
|
||||
|
||||
pub(crate) mod deserialize;
|
||||
|
||||
pub(crate) mod serialize;
|
||||
|
||||
#[derive(Default, Clone)]
|
||||
pub(crate) struct SerializeContext {
|
||||
data: Rc<RefCell<HTMLData>>,
|
||||
}
|
||||
|
||||
impl SerializeContext {
|
||||
/// Create a new entry in the data that will be sent to the client without inserting any data. Returns an id that can be used to insert data into the entry once it is ready.
|
||||
pub(crate) fn create_entry(&self) -> usize {
|
||||
self.data.borrow_mut().create_entry()
|
||||
}
|
||||
|
||||
/// Insert data into an entry that was created with [`Self::create_entry`]
|
||||
pub(crate) fn insert<T: Serialize>(&self, id: usize, value: &T) {
|
||||
self.data.borrow_mut().insert(id, value);
|
||||
}
|
||||
|
||||
/// Push resolved data into the serialized server data
|
||||
pub(crate) fn push<T: Serialize>(&self, data: &T) {
|
||||
self.data.borrow_mut().push(data);
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn use_serialize_context() -> SerializeContext {
|
||||
use_hook(|| has_context().unwrap_or_else(|| provide_context(SerializeContext::default())))
|
||||
}
|
||||
|
||||
#[derive(serde::Serialize, serde::Deserialize, Default)]
|
||||
#[serde(transparent)]
|
||||
pub(crate) struct HTMLData {
|
||||
pub data: Vec<Vec<u8>>,
|
||||
pub data: Vec<Option<Vec<u8>>>,
|
||||
}
|
||||
|
||||
impl HTMLData {
|
||||
pub(crate) fn push<T: Serialize>(&mut self, value: &T) {
|
||||
/// Create a new entry in the data that will be sent to the client without inserting any data. Returns an id that can be used to insert data into the entry once it is ready.
|
||||
pub(crate) fn create_entry(&mut self) -> usize {
|
||||
let id = self.data.len();
|
||||
self.data.push(None);
|
||||
id
|
||||
}
|
||||
|
||||
/// Insert data into an entry that was created with [`Self::create_entry`]
|
||||
pub(crate) fn insert<T: Serialize>(&mut self, id: usize, value: &T) {
|
||||
let mut serialized = Vec::new();
|
||||
serialize::serde_to_writable(value, &mut serialized).unwrap();
|
||||
self.data.push(serialized);
|
||||
ciborium::into_writer(value, &mut serialized).unwrap();
|
||||
self.data[id] = Some(serialized);
|
||||
}
|
||||
|
||||
pub(crate) fn cursor(self) -> HTMLDataCursor {
|
||||
HTMLDataCursor {
|
||||
data: self.data,
|
||||
index: AtomicUsize::new(0),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct HTMLDataCursor {
|
||||
data: Vec<Vec<u8>>,
|
||||
index: AtomicUsize,
|
||||
}
|
||||
|
||||
impl HTMLDataCursor {
|
||||
pub fn take<T: DeserializeOwned>(&self) -> Option<T> {
|
||||
let current = self.index.load(std::sync::atomic::Ordering::SeqCst);
|
||||
if current >= self.data.len() {
|
||||
tracing::error!(
|
||||
"Tried to take more data than was available, len: {}, index: {}",
|
||||
self.data.len(),
|
||||
current
|
||||
);
|
||||
return None;
|
||||
}
|
||||
let mut cursor = &self.data[current];
|
||||
let mut decoded = STANDARD.decode(cursor).unwrap();
|
||||
self.index.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
|
||||
match ciborium::from_reader(Cursor::new(decoded)) {
|
||||
Ok(x) => Some(x),
|
||||
Err(e) => {
|
||||
tracing::error!("Error deserializing data: {:?}", e);
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn serialized_and_deserializes() {
|
||||
use ciborium::{from_reader, into_writer};
|
||||
|
||||
#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)]
|
||||
struct Data {
|
||||
a: u32,
|
||||
b: String,
|
||||
bytes: Vec<u8>,
|
||||
nested: Nested,
|
||||
}
|
||||
|
||||
#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)]
|
||||
struct Nested {
|
||||
a: u32,
|
||||
b: u16,
|
||||
c: u8,
|
||||
}
|
||||
|
||||
for x in 0..10usize {
|
||||
for y in 0..10 {
|
||||
let mut as_string: Vec<u8> = Vec::new();
|
||||
let data = vec![
|
||||
Data {
|
||||
a: x as u32,
|
||||
b: "hello".to_string(),
|
||||
bytes: vec![0; x],
|
||||
nested: Nested {
|
||||
a: 1,
|
||||
b: x as u16,
|
||||
c: 3
|
||||
},
|
||||
};
|
||||
y
|
||||
];
|
||||
serialize::serde_to_writable(&data, &mut as_string).unwrap();
|
||||
|
||||
println!("{:?}", as_string);
|
||||
println!(
|
||||
"original size: {}",
|
||||
std::mem::size_of::<Data>() * data.len()
|
||||
);
|
||||
let mut bytes = Vec::new();
|
||||
into_writer(&data, &mut bytes).unwrap();
|
||||
println!("serialized size: {}", bytes.len());
|
||||
println!("compressed size: {}", as_string.len());
|
||||
|
||||
let decoded: Vec<Data> = deserialize::serde_from_bytes(&as_string).unwrap();
|
||||
assert_eq!(data, decoded);
|
||||
}
|
||||
/// Push resolved data into the serialized server data
|
||||
pub(crate) fn push<T: Serialize>(&mut self, data: &T) {
|
||||
let mut serialized = Vec::new();
|
||||
ciborium::into_writer(data, &mut serialized).unwrap();
|
||||
self.data.push(Some(serialized));
|
||||
}
|
||||
}
|
||||
@@ -1,28 +1,88 @@
|
||||
use dioxus_lib::prelude::dioxus_core::DynamicNode;
|
||||
use dioxus_lib::prelude::{
|
||||
has_context, try_consume_context, ScopeId, SuspenseBoundaryProps, VNode, VirtualDom,
|
||||
};
|
||||
use serde::Serialize;
|
||||
|
||||
use base64::engine::general_purpose::STANDARD;
|
||||
use base64::Engine;
|
||||
|
||||
use super::SerializeContext;
|
||||
|
||||
#[allow(unused)]
|
||||
pub(crate) fn serde_to_writable<T: Serialize>(
|
||||
value: &T,
|
||||
write_to: &mut impl std::io::Write,
|
||||
) -> Result<(), ciborium::ser::Error<std::io::Error>> {
|
||||
write_to: &mut impl std::fmt::Write,
|
||||
) -> Result<(), ciborium::ser::Error<std::fmt::Error>> {
|
||||
let mut serialized = Vec::new();
|
||||
ciborium::into_writer(value, &mut serialized)?;
|
||||
write_to.write_all(STANDARD.encode(serialized).as_bytes())?;
|
||||
ciborium::into_writer(value, &mut serialized).unwrap();
|
||||
write_to.write_str(STANDARD.encode(serialized).as_str())?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(feature = "server")]
|
||||
/// Encode data into a element. This is inteded to be used in the server to send data to the client.
|
||||
pub(crate) fn encode_in_element(
|
||||
data: &super::HTMLData,
|
||||
write_to: &mut impl std::io::Write,
|
||||
) -> Result<(), ciborium::ser::Error<std::io::Error>> {
|
||||
write_to.write_all(
|
||||
r#"<meta hidden="true" id="dioxus-storage-data" data-serialized=""#.as_bytes(),
|
||||
)?;
|
||||
serde_to_writable(&data, write_to)?;
|
||||
Ok(write_to.write_all(r#"" />"#.as_bytes())?)
|
||||
impl super::HTMLData {
|
||||
/// Walks through the suspense boundary in a depth first order and extracts the data from the context API.
|
||||
/// We use depth first order instead of relying on the order the hooks are called in because during suspense on the server, the order that futures are run in may be non deterministic.
|
||||
pub(crate) fn extract_from_suspense_boundary(vdom: &VirtualDom, scope: ScopeId) -> Self {
|
||||
let mut data = Self::default();
|
||||
data.take_from_scope(vdom, scope);
|
||||
data
|
||||
}
|
||||
|
||||
fn take_from_virtual_dom(&mut self, vdom: &VirtualDom) {
|
||||
self.take_from_scope(vdom, ScopeId::ROOT)
|
||||
}
|
||||
|
||||
fn take_from_scope(&mut self, vdom: &VirtualDom, scope: ScopeId) {
|
||||
vdom.in_runtime(|| {
|
||||
scope.in_runtime(|| {
|
||||
// Grab any serializable server context from this scope
|
||||
let context: Option<SerializeContext> = has_context();
|
||||
if let Some(context) = context {
|
||||
let borrow = context.data.borrow();
|
||||
let mut data = borrow.data.iter().cloned();
|
||||
self.data.extend(data)
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// then continue to any children
|
||||
if let Some(scope) = vdom.get_scope(scope) {
|
||||
// If this is a suspense boundary, move into the children first (even if they are suspended) because that will be run first on the client
|
||||
if let Some(suspense_boundary) = SuspenseBoundaryProps::downcast_from_scope(scope) {
|
||||
if let Some(node) = suspense_boundary.suspended_nodes.as_ref() {
|
||||
self.take_from_vnode(vdom, node);
|
||||
}
|
||||
}
|
||||
if let Some(node) = scope.try_root_node() {
|
||||
self.take_from_vnode(vdom, node);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn take_from_vnode(&mut self, vdom: &VirtualDom, vnode: &VNode) {
|
||||
for (dynamic_node_index, dyn_node) in vnode.dynamic_nodes.iter().enumerate() {
|
||||
match dyn_node {
|
||||
DynamicNode::Component(comp) => {
|
||||
if let Some(scope) = comp.mounted_scope(dynamic_node_index, vnode, vdom) {
|
||||
self.take_from_scope(vdom, scope.id());
|
||||
}
|
||||
}
|
||||
DynamicNode::Fragment(nodes) => {
|
||||
for node in nodes {
|
||||
self.take_from_vnode(vdom, node);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "server")]
|
||||
/// Encode data as base64. This is intended to be used in the server to send data to the client.
|
||||
pub(crate) fn serialized(&self) -> String {
|
||||
let mut serialized = Vec::new();
|
||||
ciborium::into_writer(&self.data, &mut serialized).unwrap();
|
||||
base64::engine::general_purpose::STANDARD.encode(serialized)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user