Guides & Tutorials

What are Next.js Server-Side Components

Posted on October 27, 2023 at 10:45 AM

Author's avatar

Krste Rajchevski

Software Engineer @ Bugpilot

What are Server-Side Components?

Server-Side Components (SSC) in Next.js are a new type of component specifically designed for server-side rendering. Unlike regular components in Next.js, which are rendered on the client-side, SSCs are rendered directly on the server-side. This means that the server generates the HTML for these components, which is then sent to the client.
SSCs offer a number of benefits over traditional client-side rendering. They allow for faster initial page loads, as the server can pre-render the components before sending them to the client. This can greatly improve the perceived performance of your application, especially for users with slower internet connections.

How do Server-Side Components work?

In Next.js, Server-Side Components are defined using the server-components package. This package provides a set of tools and utilities for creating SSCs. To use SSCs in your Next.js application, you need to install and import the server-components package.
Once you have imported the package, you can define an SSC by creating a new component that extends the ServerComponent base class. Within this component, you can implement the render method, which will be used to generate the HTML for the component.

1import { ServerComponent } from 'server-components';
2class MyServerComponent extends ServerComponent {
3  render() {
4    return <h1>Hello, SSC!</h1>;
5  }
6}

In the example above, we define a simple SSC called MyServerComponent that renders a heading element with the text "Hello, SSC!".
To render the SSC on the client-side, you can use the Hydrate component provided by the server-components package. This component takes care of hydrating the server-rendered HTML and turning it into an interactive component.

1import { Hydrate } from 'server-components';
2function MyApp({ page }) {
3  return (
4    <>
5      {page}
6      <Hydrate />
7    </>
8  );
9}

Using Server-Side Components in Next.js

Now that you understand the basics of Server-Side Components in Next.js, let's see how you can use them in your applications.
First, you need to install Next.js version 12 or later. You can do this by running the following command:

1npm install next@12

Once you have installed Next.js, you can start using Server-Side Components in your application. To define an SSC, create a new file with the .scomp extension and implement your component using the ServerComponent base class. Here is an example of a simple SSC:

1// components/MyServerComponent.scomp.js
2import { ServerComponent } from 'server-components';
3class MyServerComponent extends ServerComponent {
4  render() {
5    return <h1>Hello, SSC!</h1>;
6  }
7}
8export default MyServerComponent;

To use the SSC in your Next.js pages, import it and include it in your JSX code. Here is an example of how you can use the MyServerComponent in a Next.js page:

1// pages/index.js
2import MyServerComponent from '../components/MyServerComponent.scomp.js';
3export default function Home() {
4  return (
5    <div>
6      <h1>Next.js Server-Side Components</h1>
7      <MyServerComponent />
8    </div>
9  );
10}

In the example above, we import the MyServerComponent SSC and include it in the Home page component. The SSC will be rendered on the server-side and sent to the client as part of the initial HTML response.

Conclusion

Next.js Server-Side Components (SSC) are a powerful feature that allows developers to take advantage of server-side rendering in their Next.js applications. By rendering components directly on the server-side, SSCs can improve the initial page load performance and provide a better user experience. With the introduction of SSCs in Next.js version 12, developers now have an even greater capability to build high-performance web applications. So why not give Next.js SSCs a try in your next project?

Get started with Bugpilot

Bugpilot makes it easy to find and fix errors in your web apps. Our AI can find hidden user-facing bugs, and alert you before your users do.