commit f8f0a832e967260cadea8115f6b803efa4b54851
Author: kjuulh
Date: Sat Mar 4 17:42:49 2023 +0100
feat: add base
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..8cdaa33
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,13 @@
+# Generated by Cargo
+# will have compiled files and executables
+/target/
+pkg
+
+# These are backup files generated by rustfmt
+**/*.rs.bk
+
+# node e2e test tools and outputs
+node_modules/
+test-results/
+end2end/playwright-report/
+playwright/.cache/
diff --git a/Cargo.toml b/Cargo.toml
new file mode 100644
index 0000000..07a91a0
--- /dev/null
+++ b/Cargo.toml
@@ -0,0 +1,91 @@
+[package]
+name = "ssr_modes_axum"
+version = "0.1.0"
+edition = "2021"
+
+[lib]
+crate-type = ["cdylib", "rlib"]
+
+[dependencies]
+console_error_panic_hook = "0.1"
+console_log = "0.2"
+cfg-if = "1"
+lazy_static = "1"
+leptos = { path = "../../leptos", default-features = false, features = [
+ "serde",
+] }
+leptos_meta = { path = "../../meta", default-features = false }
+leptos_axum = { path = "../../integrations/axum", default-features = false, optional = true }
+leptos_router = { path = "../../router", default-features = false }
+log = "0.4"
+serde = { version = "1", features = ["derive"] }
+simple_logger = "4"
+thiserror = "1"
+axum = { version = "0.6.1", optional = true }
+tower = { version = "0.4.13", optional = true }
+tower-http = { version = "0.3.4", features = ["fs"], optional = true }
+tokio = { version = "1", features = ["time"], optional = true}
+wasm-bindgen = "0.2"
+
+[features]
+hydrate = ["leptos/hydrate", "leptos_meta/hydrate", "leptos_router/hydrate"]
+ssr = [
+ "dep:axum",
+ "dep:tower",
+ "dep:tower-http",
+ "dep:tokio",
+ "leptos/ssr",
+ "leptos_meta/ssr",
+ "leptos_router/ssr",
+ "dep:leptos_axum",
+]
+
+[package.metadata.leptos]
+# The name used by wasm-bindgen/cargo-leptos for the JS/WASM bundle. Defaults to the crate name
+output-name = "ssr_modes"
+# The site root folder is where cargo-leptos generate all output. WARNING: all content of this folder will be erased on a rebuild. Use it in your server setup.
+site-root = "target/site"
+# The site-root relative folder where all compiled output (JS, WASM and CSS) is written
+# Defaults to pkg
+site-pkg-dir = "pkg"
+# [Optional] The source CSS file. If it ends with .sass or .scss then it will be compiled by dart-sass into CSS. The CSS is optimized by Lightning CSS before being written to //app.css
+style-file = "style/main.scss"
+# Assets source dir. All files found here will be copied and synchronized to site-root.
+# The assets-dir cannot have a sub directory with the same name/path as site-pkg-dir.
+#
+# Optional. Env: LEPTOS_ASSETS_DIR.
+assets-dir = "assets"
+# The IP and port (ex: 127.0.0.1:3000) where the server serves the content. Use it in your server setup.
+site-addr = "127.0.0.1:3000"
+# The port to use for automatic reload monitoring
+reload-port = 3001
+# [Optional] Command to use when running end2end tests. It will run in the end2end dir.
+# [Windows] for non-WSL use "npx.cmd playwright test"
+# This binary name can be checked in Powershell with Get-Command npx
+end2end-cmd = "npx playwright test"
+end2end-dir = "end2end"
+# The browserlist query used for optimizing the CSS.
+browserquery = "defaults"
+# Set by cargo-leptos watch when building with that tool. Controls whether autoreload JS will be included in the head
+watch = false
+# The environment Leptos will run in, usually either "DEV" or "PROD"
+env = "DEV"
+# The features to use when compiling the bin target
+#
+# Optional. Can be over-ridden with the command line parameter --bin-features
+bin-features = ["ssr"]
+
+# If the --no-default-features flag should be used when compiling the bin target
+#
+# Optional. Defaults to false.
+bin-default-features = false
+
+# The features to use when compiling the lib target
+#
+# Optional. Can be over-ridden with the command line parameter --lib-features
+lib-features = ["hydrate"]
+
+# If the --no-default-features flag should be used when compiling the lib target
+#
+# Optional. Defaults to false.
+lib-default-features = false
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..e869ce3
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,21 @@
+MIT License
+
+Copyright (c) 2022 henrik
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
diff --git a/Makefile.toml b/Makefile.toml
new file mode 100644
index 0000000..ab91756
--- /dev/null
+++ b/Makefile.toml
@@ -0,0 +1,9 @@
+[tasks.build]
+command = "cargo"
+args = ["+nightly", "build-all-features"]
+install_crate = "cargo-all-features"
+
+[tasks.check]
+command = "cargo"
+args = ["+nightly", "check-all-features"]
+install_crate = "cargo-all-features"
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..60d7ff8
--- /dev/null
+++ b/README.md
@@ -0,0 +1,54 @@
+# Server-Side Rendering Modes
+
+This example shows the different "rendering modes" that can be used while server-side
+rendering an application:
+1. **Synchronous**: Serve an HTML shell that includes `fallback` for any `Suspense`. Load data on the client, replacing `fallback` once they're loaded.
+ - *Pros*: App shell appears very quickly: great TTFB (time to first byte).
+ - *Cons*: Resources load relatively slowly; you need to wait for JS + Wasm to load before even making a request.
+ 2. **Out-of-order streaming**: Serve an HTML shell that includes `fallback` for any `Suspense`. Load data on the **server**, streaming it down to the client as it resolves, and streaming down HTML for `Suspense` nodes.
+ - *Pros*: Combines the best of **synchronous** and **`async`**, with a very fast shell and resources that begin loading on the server.
+ - *Cons*: Requires JS for suspended fragments to appear in correct order. Weaker meta tag support when it depends on data that's under suspense (has already streamed down ``)
+ 3. **In-order streaming**: Walk through the tree, returning HTML synchronously as in synchronous rendering and out-of-order streaming until you hit a `Suspense`. At that point, wait for all its data to load, then render it, then the rest of the tree.
+ - *Pros*: Does not require JS for HTML to appear in correct order.
+ - *Cons*: Loads the shell more slowly than out-of-order streaming or synchronous rendering because it needs to pause at every `Suspense`. Cannot begin hydration until the entire page has loaded, so earlier pieces
+ of the page will not be interactive until the suspended chunks have loaded.
+ 4. **`async`**: Load all resources on the server. Wait until all data are loaded, and render HTML in one sweep.
+ - *Pros*: Better handling for meta tags (because you know async data even before you render the ``). Faster complete load than **synchronous** because async resources begin loading on server.
+ - *Cons*: Slower load time/TTFB: you need to wait for all async resources to load before displaying anything on the client.
+
+## Server Side Rendering with `cargo-leptos`
+`cargo-leptos` is now the easiest and most featureful way to build server side rendered apps with hydration. It provides automatic recompilation of client and server code, wasm optimisation, CSS minification, and more! Check out more about it [here](https://github.com/akesson/cargo-leptos)
+
+1. Install cargo-leptos
+```bash
+cargo install --locked cargo-leptos
+```
+2. Build the site in watch mode, recompiling on file changes
+```bash
+cargo leptos watch
+```
+
+Open browser on [http://localhost:3000/](http://localhost:3000/)
+
+3. When ready to deploy, run
+```bash
+cargo leptos build --release
+```
+
+## Server Side Rendering without cargo-leptos
+To run it as a server side app with hydration, you'll need to have wasm-pack installed.
+
+0. Edit the `[package.metadata.leptos]` section and set `site-root` to `"."`. You'll also want to change the path of the `` component in the root component to point towards the CSS file in the root. This tells leptos that the WASM/JS files generated by wasm-pack are available at `./pkg` and that the CSS files are no longer processed by cargo-leptos. Building to alternative folders is not supported at this time. You'll also want to edit the call to `get_configuration()` to pass in `Some(Cargo.toml)`, so that Leptos will read the settings instead of cargo-leptos. If you do so, your file/folder names cannot include dashes.
+1. Install wasm-pack
+```bash
+cargo install wasm-pack
+```
+2. Build the Webassembly used to hydrate the HTML from the server
+```bash
+wasm-pack build --target=web --debug --no-default-features --features=hydrate
+```
+3. Run the server to serve the Webassembly, JS, and HTML
+```bash
+cargo run --no-default-features --features=ssr
+```
+
diff --git a/assets/favicon.ico b/assets/favicon.ico
new file mode 100644
index 0000000..2ba8527
Binary files /dev/null and b/assets/favicon.ico differ
diff --git a/src/app.rs b/src/app.rs
new file mode 100644
index 0000000..9634346
--- /dev/null
+++ b/src/app.rs
@@ -0,0 +1,189 @@
+use lazy_static::lazy_static;
+use leptos::*;
+use leptos_meta::*;
+use leptos_router::*;
+use serde::{Deserialize, Serialize};
+use thiserror::Error;
+
+#[component]
+pub fn App(cx: Scope) -> impl IntoView {
+ // Provides context that manages stylesheets, titles, meta tags, etc.
+ provide_meta_context(cx);
+
+ view! { cx,
+
+
+
+
+
+
+ // We’ll load the home page with out-of-order streaming and
+ }/>
+
+ // We'll load the posts with async rendering, so they can set
+ // the title and metadata *after* loading the data
+ }
+ ssr=SsrMode::Async
+ />
+ }
+ ssr=SsrMode::InOrder
+ />
+
+
+
+ }
+}
+
+#[component]
+fn HomePage(cx: Scope) -> impl IntoView {
+ // load the posts
+ let posts =
+ create_resource(cx, || (), |_| async { list_post_metadata().await });
+ let posts_view = move || {
+ posts.with(cx, |posts| posts
+ .clone()
+ .map(|posts| {
+ posts.iter()
+ .map(|post| view! { cx,
+
+ // since we're using async rendering for this page,
+ // this metadata should be included in the actual HTML
+ // when it's first served
+
+
+ }
+ })
+ })
+ };
+
+ view! { cx,
+ "Loading post..." }>
+
+