Let's create a new page that fetches user data using SSR. Create a new file named users.tsx in the pages directory and add the following code:
1// pages/users.tsx
2
3import React from 'react';
4
5// Type definition for User
6interface User {
7 id: number;
8 name: string;
9 email: string;
10}
11
12// Function to fetch users from an API
13async function fetchUsers(): Promise<User[]> {
14 const response = await fetch('https://jsonplaceholder.typicode.com/users');
15 const data = await response.json();
16 return data;
17}
18
19// Default export of the SSR page component
20export default async function UsersPage() {
21 const users = await fetchUsers(); // Fetch users on the server
22
23 return (
24 <div>
25 <h1>User List</h1>
26 <ul>
27 {users.map(user => (
28 <li key={user.id}>
29 <strong>{user.name}</strong> - {user.email}
30 </li>
31 ))}
32 </ul>
33 </div>
34 );
35}