CORE CONCEPTS
Client-Side Rendering (CSR) is a technique where the initial HTML sent to the browser is minimal, and JavaScript dynamically fetches and renders content after the page loads. This approach is commonly used in modern single-page applications (SPAs) and provides:
However, CSR has some trade-offs:
By default, pages in Rasengan.js use Server-Side Rendering (SSR).
However, you can opt-in to Client-Side Rendering (CSR) by setting ssr: false in the rasengan.config.js file.
jsimport { defineConfig } from "rasengan"; export default defineConfig({ ssr: false });
By setting ssr: false, you're disabling SSR and opting into CSR. By doing this, you will have the following behavior:
In CSR, data fetching happens inside the component using hooks like useEffect.
tsximport { PageComponent } from "rasengan"; import { useEffect, useState } from "react"; const Profile: PageComponent = () => { const [user, setUser] = useState<{ name: string; email: string } | null>(null); useEffect(() => { fetch("https://api.example.com/user") .then(res => res.json()) .then(data => setUser(data)); }, []); if (!user) return <p>Loading...</p>; return ( <div> <h1>{user.name}</h1> <p>{user.email}</p> </div> ); }; Profile.path = "/profile"; Profile.metadata = { title: "User Profile", description: "View your profile information", }; export default Profile;
<p>Loading...</p>).useEffect hook runs and fetches user data.✅ Use CSR when:
❌ Avoid CSR when:
| Feature | Client-Side Rendering (CSR) | Server-Side Rendering (SSR) |
|---|---|---|
| Initial Load Speed | Slower (JS must load first) | Faster (HTML is pre-rendered) |
| SEO | Difficult (content appears later) | Excellent (content is ready at load) |
| Interactivity | Great for dynamic UI updates | Requires hydration after load |
| Server Load | Lower | Higher |
CSR is ideal when data needs to be fetched based on user actions.
tsximport { PageComponent } from "rasengan"; import { useState } from "react"; const Search: PageComponent = () => { const [query, setQuery] = useState(""); const [results, setResults] = useState<{ name: string }[]>([]); const handleSearch = async () => { const data = await fetch(`https://api.example.com/search?q=${query}`).then(res => res.json()); setResults(data); }; return ( <div> <input type="text" placeholder="Search..." value={query} onChange={(e) => setQuery(e.target.value)} /> <button onClick={handleSearch}>Search</button> <ul> {results.map((item, index) => ( <li key={index}>{item.name}</li> ))} </ul> </div> ); }; Search.path = "/search"; Search.metadata = { title: "Search", description: "Search for items in our database", }; export default Search;
Client-Side Rendering in Rasengan.js provides a fast and interactive experience once the page is loaded. It is best for dynamic applications where SEO is not a primary concern.