SSR | CSR

Client-side vs server-side rendering in SvelteKit: how each works, when to pick which, and what each one costs.

Understanding CSR and SSR in SvelteKit

When developing web applications with SvelteKit, choosing the right rendering strategy is crucial for performance and user experience. The two main rendering methods are Client-Side Rendering (CSR) and Server-Side Rendering (SSR). Below is an overview of each.

Time to first contentABC1234CSRSSRbrowserGET /serverempty shell + JS02browserdownload · run JS03fetch · renderclient side04content visibleafter JSbrowserGET /serverload() · render07content visiblefirst paintdownload JSin background09interactivehydrated10full HTMLhydrate
FIG. 1Time to first content

Client-Side Rendering (CSR)

Definition: CSR renders content entirely in the browser using JavaScript. The server sends a minimal HTML shell, and the JavaScript bundle takes over to render the full content on the client side.

CSR · the browser does the renderingABC123BROWSERuserGET /pageweb serverempty HTML shell02JS bundledownloadbrowser JSexecute bundle04DOMrendered client side05page visibleand interactiveAPIJSON data07fetchrender
FIG. 2CSR · the browser does the rendering

SvelteKit Example:

To enable CSR in a SvelteKit route:

spec · javascript
// src/routes/+page.server.js
export const ssr = false; // Disables server-side rendering
spec · svelte
<!-- src/routes/+page.svelte -->
<script>
  // Client-side logic
</script>

<h1>Welcome to Client-Side Rendering</h1>

Advantages:

  • Reduced load on the server.
  • Smooth and fast client-side transitions.

Disadvantages:

  • Slower initial load times due to JavaScript loading.
  • Content is not available until JavaScript is loaded and executed.

Server-Side Rendering (SSR)

Definition: SSR generates HTML content on the server for each request. The fully rendered HTML is sent to the client, providing faster initial load times and allowing for better data protection.

SSR · the server sends finished HTMLABC123SERVERuserGET /pageSvelteKitroute + hooks02load()+page.server.js03renderHTML on server04database / APIprivate datafirst paintfull HTMLhydrationJS takes over07dataHTML
FIG. 3SSR · the server sends finished HTML

SvelteKit Example:

Default behavior in SvelteKit (SSR enabled):

spec · javascript
// src/routes/+page.server.js
export const ssr = true; // Enable server-side rendering
spec · js
// src/routes/+page.server.js
export async function load() {
  return {
    data: { title: "Server-Rendered Page" },
  };
}
spec · svelte
<!-- src/routes/+page.svelte -->
<script>
  export let data;
</script>

<h1>{data.title}</h1>

Advantages:

  • Faster initial rendering.
  • Better security control, as you can protect routes and sensitive data on the server.
  • Sensitive data is not exposed on the client.

Disadvantages:

  • Increased load on the server.
  • Potential increases in hosting costs due to additional processing.

Conclusion

The choice between CSR and SSR depends on the specific needs of your application:

  • CSR is ideal for highly interactive applications where initial load time is not critical and no additional server-side data protection is required.
  • SSR offers a balance between interactivity and performance and is suitable for dynamic content that requires greater security in protecting routes and sensitive data.

By understanding these rendering strategies and leveraging SvelteKit's flexibility, you can optimize your web application to improve performance, user experience, and the security of your data and routes when necessary.