Next 13 – Server and Client components

next-13-–-server-and-client-components

With Next 13 the team made quite big changes regarding how components render.
Not that it’s all-new, but they seem to have simplified it and changed the defaults.

So let’s take a closer look at how rendering works in Next 13.

What is Server vs. Client?

We’ll first have to address what is seen as the server and the client.

  • Server: A actual server that hosts your code can receive a request, do some calculations and send back a response once those are complete.
  • Client: The browser a user’s device sends a request to the server and does some calculations in the browser to render the correct layout.

As you can see, the tipping point lies in where the calculations happen.

So what exactly did the ecosystem look like before Next 13?

In general, with React, everything happens on the client side. Next solve this by breaking your React components down into pages that could partially be rendered on the server.
However, this generated HTML on the server, which had to be hydrated on the client again, meaning we needed extra JavaScript.

With the introduction of the clear distinguishment between server and client-side components, React can render on the server OR the client.

The new default in all this is server-side rendering.

Server side: Static or Dynamic rendering

With this addition of being Server side first, we get another two choices.
Either we render statically or dynamically.

Let’s take a closer look at what that means.

  • Static: Both server and client components can be prerendered on the server at build time. This will cache the result and reuse that cache on the following requests. (Equivalent of SSG and ISR in pre-13 terms)
  • Dynamic: Server and client components are rendered on request and will not be cached. (Equivalent of SSR pre-13 terms)

How to create client components?

You might wonder, if these server components are the new default, how do I use client components?

And Next makes this super simple by adding a directive at the top of your file.

'use client';

export default function YourPage() {
  //Client-side code
}

Yep, that directive will take care of everything!

When to use which one?

It might feel tricky to know which one to use at which stage, so let’s look at Next’s directive on this.

Let’s break it down by what you might need.

  • Fetching data? -> Server component
  • Access backend resources -> Server component
  • Keep sensitive information on the server -> Server component
  • Reduce JavaScript code -> Server component

  • Add interactivity (click/change listeners) -> Client component

  • Use state or lifecycle effects -> Client component

  • Browser-only APIs -> Client component

  • Custom hooks that need any of the above -> Client components

You might think, ok but surely they need to mix and match, and indeed they do.

The advice given by Next is that we opt to make component server-side where possible and extract the ones that need client-side rendering.

This way, our server components only need to render that specific small component on the client.
But more on this later on when we try them out.

Thank you for reading, and let’s connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post
hello-world-with-react

Hello World with React

Next Post
what-is-blockchain

What is Blockchain

Related Posts
在termux中安装和使用google-gemini-cli的完整指南

在Termux中安装和使用Google Gemini CLI的完整指南

什么是Google Gemini CLI? Google Gemini CLI是一个命令行工具,允许开发者直接在终端中与Google的Gemini AI模型交互。它提供了简单高效的方式来测试和集成Gemini的强大AI能力到你的开发工作流中。 Gemini是Google最新推出的大型语言模型,具有强大的自然语言理解和生成能力,可以用于代码生成、问题解答、内容创作等多种场景。 在Termux中安装Gemini CLI Termux是Android设备上的强大终端模拟器,下面我们一步步教你如何在Termux中安装和使用Gemini CLI。 1. 准备工作 首先确保你的Termux是最新版本,并更新软件包: pkg update &&…
Read More