-
Notifications
You must be signed in to change notification settings - Fork 135
Description
I'm trying to port my app from client side rendering CSR into server side rendering SSR with the help of this repository.
The documentation in https://book.leptos.dev/ssr/index.html is fairly limited, for instance:
- https://book.leptos.dev/server/25_server_functions.html (this got me confused because i wonder: is this about SSR or any other server function?) Please add a sentence at the top like:
(as outlined with the red error, move this text down) and extend this text:
Leptos is one of a number of modern frameworks that introduce the concept of server functions. Server side rendering (SSR) is archived with a combination of a server function and a leptos component using it.
Concepts to teach
Using SSR means that we still work on a GET request basis (like in PHP) so we can't use signals the way we can in CSR mode. SSR still supports the concept of signals, but they are on a 'per-request-basis' and have a rather short time-span to live.
You can use leptos with CSR without accessing remote resources this way you can store data in the localStore of your browser. If you want to access remote data from a server in CSR, these are your options:
- you access a REST interface, like a JSON API
- you can use Websockets
Leptos with SSR offers these concepts:
- on the server, we read/write to and from a database using crates like sqlx, and create the webpage based on that. Page updates are explicitly done when the user using F5 (reload). This shows changes other users caused to the database.
See https://github.com/leptos-rs/leptos/blob/main/examples/todo_app_sqlite_axum/src/todo.rs - or we can stream updates using websockets! See https://github.com/leptos-rs/leptos/tree/main/examples/websocket
Create new example from examples
I'm coming from leptos CSR with server data coming via REST. The leptos SSR concepts are new for me and I am used to:
- passing huge amounts of RwSignals/Signals down different routes (they embody the global state of the application and can be updated using calls to external REST) - SSR would use a database for global state and WS for server push.
- I use
<Router><Routes fallback=|| "Not found."> ...but leptos SSR requires a rewrite into rust functions.
But reading https://github.com/leptos-rs/leptos/blob/main/examples/websocket confuses me greatly:
- Having main.rs, lib.rs and websocket.rs with all the different entry points
- The SSR router works differently than CSR
Extending the book
The book should be a continuous story of small extensions to the template starting with https://github.com/leptos-rs/start-axum-workspace and then:
- create a router 2 "/" (home) and "/todos"
- extend it with a function
fn gen_content_from_server()which is running on the server only and called from a component on the UI - extend the server core routine with a context, for server variables and then use that in
fn gen_content_from_server() - create a function which is only evaluated on the client
- extend the server with a db like sqlite and extend
fn gen_content_from_server()to get data from the db - create
fn my_editor_componentwhich loads and saves data from a fn on the server but uses a client side only editor - extend server and client with a websocket and have a todo list which always updates all devices in sync