-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
SSR Hydration #2552
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SSR Hydration #2552
Changes from all commits
5ce4df6
e9b8aa0
dd209e4
7c5a5be
b62f347
02b21bc
bfc7a0f
7f9eae6
7feac4a
f0cd363
b1377bd
09e4275
80eb187
4a653c8
1805c49
a008b5b
3f0ec59
fb65153
cf5f99d
383d6b7
e105e28
23937ff
137f89b
054026d
5fb2ed8
9b23b55
7065ac9
eb5631d
236baa1
6cb88c9
95889da
6d4c364
ffd05f5
bfef5d3
b5dcd2f
9555d4a
5c86c50
73280cd
a339020
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,7 +11,7 @@ | |
| href="https://cdn.jsdelivr.net/npm/[email protected]/css/bulma.min.css" | ||
| /> | ||
| <link data-trunk rel="sass" href="index.scss" /> | ||
| <link data-trunk rel="rust" data-cargo-features="csr" /> | ||
| <link data-trunk rel="rust" data-cargo-features="csr" data-bin="function_router" /> | ||
| </head> | ||
|
|
||
| <body></body> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| pub use function_router::*; | ||
|
|
||
| fn main() { | ||
| wasm_logger::init(wasm_logger::Config::new(log::Level::Trace)); | ||
| #[cfg(feature = "csr")] | ||
| yew::Renderer::<App>::new().render(); | ||
| } |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="utf-8" /> | ||
| <title>Yew SSR Example</title> | ||
|
|
||
| <link data-trunk rel="rust" data-bin="simple_ssr_hydrate" /> | ||
| </head> | ||
| </html> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| use simple_ssr::App; | ||
|
|
||
| fn main() { | ||
| #[cfg(target_arch = "wasm32")] | ||
| wasm_logger::init(wasm_logger::Config::new(log::Level::Trace)); | ||
| yew::Renderer::<App>::new().hydrate(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| use clap::Parser; | ||
| use once_cell::sync::Lazy; | ||
| use simple_ssr::App; | ||
| use std::path::PathBuf; | ||
| use tokio_util::task::LocalPoolHandle; | ||
| use warp::Filter; | ||
|
|
||
| // We spawn a local pool that is as big as the number of cpu threads. | ||
| static LOCAL_POOL: Lazy<LocalPoolHandle> = Lazy::new(|| LocalPoolHandle::new(num_cpus::get())); | ||
|
|
||
| /// A basic example | ||
| #[derive(Parser, Debug)] | ||
| struct Opt { | ||
| /// the "dist" created by trunk directory to be served for hydration. | ||
| #[structopt(short, long, parse(from_os_str))] | ||
| dir: PathBuf, | ||
| } | ||
|
|
||
| async fn render(index_html_s: &str) -> String { | ||
| let content = LOCAL_POOL | ||
| .spawn_pinned(move || async move { | ||
| let renderer = yew::ServerRenderer::<App>::new(); | ||
|
|
||
| renderer.render().await | ||
| }) | ||
| .await | ||
| .expect("the task has failed."); | ||
|
|
||
| // Good enough for an example, but developers should avoid the replace and extra allocation | ||
| // here in an actual app. | ||
| index_html_s.replace("<body>", &format!("<body>{}", content)) | ||
| } | ||
|
|
||
| #[tokio::main] | ||
| async fn main() { | ||
| let opts = Opt::parse(); | ||
|
|
||
| let index_html_s = tokio::fs::read_to_string(opts.dir.join("index.html")) | ||
| .await | ||
| .expect("failed to read index.html"); | ||
|
|
||
| let html = warp::path::end().then(move || { | ||
| let index_html_s = index_html_s.clone(); | ||
|
|
||
| async move { warp::reply::html(render(&index_html_s).await) } | ||
| }); | ||
|
|
||
| let routes = html.or(warp::fs::dir(opts.dir)); | ||
|
|
||
| println!("You can view the website at: http://localhost:8080/"); | ||
|
|
||
| warp::serve(routes).run(([127, 0, 0, 1], 8080)).await; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| [package] | ||
| name = "ssr_router" | ||
| version = "0.1.0" | ||
| edition = "2021" | ||
|
|
||
| # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
|
||
| [dependencies] | ||
| yew = { path = "../../packages/yew", features = ["ssr", "hydration", "trace_hydration"] } | ||
| function_router = { path = "../function_router" } | ||
| log = "0.4" | ||
|
|
||
| [target.'cfg(target_arch = "wasm32")'.dependencies] | ||
| wasm-bindgen-futures = "0.4" | ||
| wasm-logger = "0.2" | ||
|
|
||
| [target.'cfg(not(target_arch = "wasm32"))'.dependencies] | ||
| tokio = { version = "1.15.0", features = ["full"] } | ||
| warp = "0.3" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The other example uses warp so how about we use axum here? That would show usage with different libraries and should also demonstrate usage with tower
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried to rewrite this example with
Doc example I am referring to: https://docs.rs/tower-http/latest/tower_http/services/fs/struct.ServeDir.html#handling-files-not-found There does not seem to be a way that allows both Related: tower-rs/tower-http#240
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @hamza1311 Do you know how to do this with axum?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I haven't tried it. I will give it a shot later. We can merge it in as-is and I will hopefully update it soon. I'm probably going to need some help from the #axum in tokio's server though... |
||
| env_logger = "0.9" | ||
| num_cpus = "1.13" | ||
| tokio-util = { version = "0.7", features = ["rt"] } | ||
| once_cell = "1.5" | ||
| clap = { version = "3.1.7", features = ["derive"] } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| # SSR Router Example | ||
|
|
||
| This example is the same as the function router example, but with | ||
| server-side rendering and hydration support. It reuses the same codebase | ||
| of the function router example. | ||
|
|
||
| # How to run this example | ||
|
|
||
| 1. Build Hydration Bundle | ||
|
|
||
| `trunk build examples/ssr_router/index.html` | ||
|
|
||
| 2. Run the server | ||
|
|
||
| `cargo run --bin ssr_router_server -- --dir examples/ssr_router/dist` | ||
|
|
||
| 3. Open Browser | ||
|
|
||
| Navigate to http://localhost:8080/ to view results. |
Uh oh!
There was an error while loading. Please reload this page.