Fun With Next.js 13 Server Components

Read Time:5 Minute, 19 Second

Next.js 13 has landed in a somewhat confusing way. Many remarkable things have been added; however, a good part is still Beta. Nevertheless, the Beta features give us important signals on how the future of Next.js will be shaped, so there are good reasons to keep a close eye on them, even if you’re going to wait to adopt them.

This article is part of a series of experiences about the Beta features. Let’s play with Server Components today.

Making server components the default option is arguably the boldest change made in Next.js 13. The goal of server component is to reduce the size of JS shipped to the client by keeping component code only on the server side. I.e., the rendering happens and only happens on the server side, even if the loading of the component is triggered on the client side (via client-side routing). It’s quite a big paradigm shift.

I first got to know React Server Components over a year ago from this video (watch later, it’s pretty long 😄):

It feels quite “reasearch-y” by then, so I was shocked when seeing that Next.js is already betting its future on it now. Time flies and the fantastic engineers from React must have done some really great work, so I created a shiny new Next.js 13 project to play with it.

Enter fullscreen mode Exit fullscreen mode

Let’s have some fun playing with the project. You can find the full project code here.

Server Component

The first difference noticed is that a new app folder now sits along with our old friend page. I’ll save the routing changes to another article, but what’s worth mentioning for now is that every component under the app folder is, by default, a server component, meaning that it’s rendered on the server side, and its code stays on the server side.

Let’s create our very first server component now:

Enter fullscreen mode Exit fullscreen mode

If you access the /server route, whether by a fresh browser load or client-side routing, you’ll only see the line of log printed in your server console but never in the browser console. The environment variable value is fetched from the server side as well.

Looking at network traffic in the browser, you’ll see the content of the Server component is loaded via a remote call which returns an octet stream of JSON data of the render result:

Enter fullscreen mode Exit fullscreen mode

Rendering a server component is literally an API call to get serialized virtual DOM and then materialize it in the browser.

The most important thing to remember is that server components are for rendering non-interactive content, so there are no event handlers, no React hooks, and no browser-only APIs.

The most significant benefit is you can freely access any backend resource and secrets in server components. It’s safer (data don’t leak) and faster (code doesn’t leak).

Client Component

To make a client component, you’ll need to mark it so explicitly with use client:

Enter fullscreen mode Exit fullscreen mode

As you may already anticipate, this gives you a similar behavior to the previous Next.js versions. When the page is first loaded, it’s rendered by SSR, so you should see the first log in the server console; during client-side routing, both log messages will appear in the browser console.

Mix and Match

One of the biggest differences between server component and SSR is that SSR is at page level, while Server Component, as its name says, is at component level. This means you can mix and match server and client components in a render tree as you wish.

Enter fullscreen mode Exit fullscreen mode

Image mixing client and server components

In a mixed scenario like this, server and client components get rendered independently, and the results are assembled by React runtime. Props passed from server components to client ones are serialized across the network (and need to be serializable).

Server Components Can Degenerate

One caution you need to take is that if a server component is directly imported into a client one, it silently degenerates into a client component.

Let’s revise the previous example slightly to observe it:

Enter fullscreen mode Exit fullscreen mode

Enter fullscreen mode Exit fullscreen mode

If you check out the log, you’ll see NestedServer has “degenerated” and is now rendered by the browser.

Is It a Better Future?

Next.js is trying everything it can to move things to the server side, exactly how people did web development two decades ago 😄. So now we’re completing a full circle, but with greatly improved development experiences and end-user experiences.

For the end users, it’s a clear win since computing on the server side is faster and more reliable. The result will be a much more rapid first content paint.

For developers, the paradigm shift will be mentally challenging, mixed with confusion, bugs, and anti-patterns. It will be a hell of a journey.

In the End

Thank you for your time reading to the end ❤️.

This is the first article of a series about the Beta features of Next.js 13. If you like our posts, remember to follow us for updates at the first opportunity.

ZenStack is a schema-first toolkit for building CRUD services in Next.js projects. Our goal is to let you save time writing boilerplate code and focus on building what matters – the user experience.

Find us on GitHub:

A schema-first toolkit for building CRUD services in Next.js projects

📣 Our Discord Server is Live

JOIN US to chat about questions, bugs, plans, or anything off the top of your head.

What is ZenStack?

ZenStack Full-stack Development Toolkit Introduction

ZenStack is a toolkit for modeling data and access policies in full-stack development with Next.js and Typescript. It takes a schema-first approach to simplify the construction of CRUD services.

Next.js is an excellent full-stack framework. However, building the backend part of a web app is still quite challenging. For example, implementing CRUD services efficiently and securely is tricky and not fun.

ZenStack simplifies it by providing:

  • An intuitive data modeling language for defining data types, relations, and access policies

Enter fullscreen mode Exit fullscreen mode


WP Ad Inserter plugin for WordPress