CORE CONCEPTS
Server-Side Rendering (SSR) is a rendering technique where pages are pre-rendered on the server and sent as fully formed HTML to the client. This approach offers several benefits:
However, SSR also introduces some trade-offs, such as increased server load and potential latency depending on data-fetching times.
Rasengan.js supports SSR by default for all pages. When a user visits a page, the server fetches the necessary data and renders the page on the server, sending the fully rendered HTML to the client.
This behavior can be disabled by setting ssr: false in the rasengan.config.js file.
jsimport { defineConfig } from "rasengan"; export default defineConfig({ ssr: true, // default value is set to true });
Rasengan.js allows you to use SSR with the loader function inside your page component. This function runs on the server and fetches data before rendering the page.
tsximport { PageComponent } from "rasengan"; type Post = { id: string; title: string; content: string; } const Posts: PageComponent<{ posts: Post[] }> = ({ posts }) => { return ( <div> { posts.map(post => ( <div key={post.id}> <h2>{post.title}</h2> <p>{post.content}</p> </div> )) } </div> ); }; Posts.path = "/posts"; Posts.metadata = { title: "Posts", description: "Explore our posts", }; // Fetch posts data on the server before rendering the page Posts.loader = async () => { const posts = await fetch(`https://api.example.com/posts`).then(res => res.json()); return { props: { posts } }; // Ensure the returned object contains `props` }; export default Posts;
/profile/123, Rasengan.js executes the loader function before rendering the page.loader function retrieves user data from an API using the params.id from the URL.The loader function is especially useful for dynamic routes. You can use URL parameters to fetch the necessary data.
tsximport { PageComponent } from "rasengan"; const Profile: PageComponent<{ user: { name: string; email: string } }> = ({ user }) => { return ( <div> <h1>{user.name}</h1> <p>{user.email}</p> </div> ); }; Profile.path = "/profile/:id"; Profile.metadata = { title: "User Profile", description: "View user details", }; // Fetch user data on the server before rendering the page Profile.loader = async ({ params }) => { const user = await fetch(`https://api.example.com/users/${params.id}`).then(res => res.json()); return { props: { user } }; // Ensure the returned object contains `props` }; export default Profile;
✅ Use SSR when:
❌ Avoid SSR when: