In simple terms, getServerSideProps enables a page to render server-side.
This tutorial shows how to convert an individual Reactjs page to a server-side rendered Next js page. Note that you do not have to convert the entire page, only the portion of a page you want server-side rendered. You can use Reactjs and Nextjs on the same page. However, the project should follow the Nextjs folder structure and architecture. Only the content rendered by Nextjs will be hydrated.
Below is a simple application page that we have converted to server-side render with the
import { useEffect, useState } from 'react'
export default function Home({ users }) {
const[reactData, setReactData] = useState([]);
useEffect(() => {
fetch('https://jsonplaceholder.typicode.com/users')
.then(res => res.json())
.then(data => {
setReactData(data);
}).catch((e) => {console.log(e)});
}, []);
return (
<>
<table>
<tr>
<th colSpan='3' className='topnav'>Rendered By React JS | Client side rendered</th>
</tr>
{reactData.map((user, index) => (
<tr>
<td>{user.name}</td>
<td>{user.username}</td>
<td>{user.email}</td>
</tr>
))}
</table>
<br/>
<table>
<tr>
<th colSpan='3' className='topnav'>Rendered By Next JS | Server side rendered</th>
</tr>
{users.map((user, index) => (
<tr>
<td>{user.name}</td>
<td>{user.username}</td>
<td>{user.email}</td>
</tr>
))}
</table>
</>
)
}
export async function getServerSideProps({params,req,res,query,preview,previewData,resolvedUrl,locale,locales,defaultLocale}) {
console.log('Logging : '+res);
const data = await fetch('https://jsonplaceholder.typicode.com/users');
const users = await data.json();
return { props: { users } }
}
Note that the first table is populated with Reactjs using the
<table>
<tr><th colSpan="3" class="topnav">Rendered By React JS | Client side rendered</th></tr>
</table>
The second table is rendered with the properties returned by Nextjs's
<table>
<tr><th colSpan="3" class="topnav">Rendered By Next JS | Server side rendered</th></tr>
<tr><td>Leanne Graham</td><td>Bret</td><td>Sincere@april.biz</td></tr>
<tr><td>Ervin Howell</td><td>Antonette</td><td>Shanna@melissa.tv</td></tr>
<tr><td>Clementine Bauch</td><td>Samantha</td><td>Nathan@yesenia.net</td></tr>
<tr><td>Patricia Lebsack</td><td>Karianne</td><td>Julianne.OConner@kory.org</td></tr>
<tr><td>Chelsey Dietrich</td><td>Kamren</td><td>Lucio_Hettinger@annie.ca</td></tr>
<tr><td>Mrs. Dennis Schulist</td><td>Leopoldo_Corkery</td><td>Karley_Dach@jasper.info</td></tr>
<tr><td>Kurtis Weissnat</td><td>Elwyn.Skiles</td><td>Telly.Hoeger@billy.biz</td></tr>
<tr><td>Nicholas Runolfsdottir V</td><td>Maxime_Nienow</td><td>Sherwood@rosamond.me</td></tr>
<tr><td>Glenna Reichert</td><td>Delphine</td><td>Chaim_McDermott@dana.io</td></tr>
<tr><td>Clementina DuBuque</td><td>Moriah.Stanton</td><td>Rey.Padberg@karina.biz</td></tr>
</table>
Now, let's look at some details about the
export async function getServerSideProps(context) {
return { props: {},}
}
The getServerSideProps() has several parameters to accept arguments.
Below is an example of
export async function getServerSideProps({params,req,res,query,preview,previewData,resolvedUrl,locale,locales,defaultLocale}) {
if (query.text) {
return { redirect: { destination: '/post', permanent: false, },}
}
const data = await fetch('https://jsonplaceholder.typicode.com/users');
const users = await data.json();
if (!data) {
return {notFound: true,}
}
return { props: { users } }
}