Streaming
Sending a page in the order its parts are ready.
A render is synchronous: it needs every value before it starts. A page whose parts arrive at different times (the book now, its reviews after a slow lookup) can still be sent as soon as possible, with holes.
hole() is a Slot whose content you write later. Render the whole page once, with a hole in each slow slot, and cut the output with split_holes: there is one more piece than there were holes. Send the first piece at once, then each hole's content when it is ready, each followed by the next piece.
// BookPage.vue: <article><h1>{{ title }}</h1><section><slot name="reviews">loading</slot></section></article>
let props = book_page::Props { title: "Dune".into() };
let mut page = String::from("<!doctype html><body>");
book_page::render(&mut page, &props, book_page::Slots { reviews: Some(ferrovue::page::hole()) });
page.push_str("</body>");
let pieces = ferrovue::page::split_holes(&page);
assert_eq!(
pieces,
[
"<!doctype html><body><article><h1>Dune</h1><section><!--[-->",
"<!--]--></section></article></body>",
]
);
// Stream: the first piece now, then the reviews when they arrive, then the rest.
let mut sent = Vec::new();
sent.push(pieces[0].to_owned());
sent.push(String::from("<ul><li>A classic.</li></ul>")); // the slow part, rendered when ready
sent.push(pieces[1].to_owned());
assert_eq!(
sent.concat(),
"<!doctype html><body><article><h1>Dune</h1><section><!--[--><ul><li>A classic.</li></ul><!--]--></section></article></body>"
);A hole is content to its slot, so the slot's fallback never shows, and the fragment markers Vue writes around slot content are already in place. What goes in a hole must be what the client's template would render in that slot, usually another component's render or island.
With the stream feature, which axum and actix-web turn on, HtmlStream does this for a response: give it the whole render and a future for each hole's content, and it sends each piece and each hole's content as soon as they are ready, the futures all running at once. Each future is polled again only once its own waker wakes, so a page is sent in time linear in its number of holes. This is the handler from the repository's examples/fullstack (src/main.rs), where site.page renders a page with its holes and site.fill renders one hole's content once its data is ready:
use ferrovue::web::HtmlStream;
let Page { status, html, holes } = site.page(uri.path());
let body = HtmlStream::new(html).holes(holes.into_iter().map(|hole| {
let site = Arc::clone(&site);
async move { site.fill(hole).await }
}));
let status = StatusCode::from_u16(status).unwrap_or(StatusCode::OK);
(status, body).into_response()web_frameworks has the rest: a whole page, other statuses, actix-web, and other servers.
A hole is written as a marker, <fv-hole"</script</style</noscript>, which no value ferrovue writes can hold: every interpolated value has its < escaped, the page head's attribute values have their " escaped, and its <script>, <style> and <noscript> content has its closing tag rewritten, so only a hole produces it, wherever data from readers goes. Do not write the marker in content of your own, such as a slot closure, or the page will be cut there too.