|
1 | | -# **Assignment 2: Server-Side Rendering and Data Fetching** |
| 1 | +# **Assignment 2: Server-Side Rendering (SSR) and Data Fetching** |
2 | 2 |
|
3 | 3 | ## **Learning Objectives** |
4 | | -1. Understand Server-Side Rendering (SSR) and Static Site Generation (SSG). |
5 | | -2. Learn how to fetch and display data using Next.js functions. |
6 | | -3. Implement Incremental Static Regeneration (ISR) for dynamic updates. |
| 4 | +- Understand SSR in Next.js. |
| 5 | +- Learn how to fetch data during server-side rendering. |
| 6 | +- Display fetched data on a page. |
7 | 7 |
|
8 | 8 | --- |
9 | 9 |
|
10 | 10 | ## **Step-by-Step Guide** |
11 | 11 |
|
12 | | -### **1. Server-Side Rendering (SSR)** |
13 | | -SSR allows you to fetch data on the server for every request. |
14 | | - |
15 | | -#### **Task: Create a Users Page** |
16 | | -- Add a file: `src/app/users/page.tsx`. |
17 | | -- Use the `getServerSideProps` function to fetch user data. |
18 | | -- Display the fetched data in a list format. |
19 | | - |
20 | | -##### Code Hint: |
21 | | -```tsx |
22 | | -// src/app/users/page.tsx |
23 | | -export default async function UsersPage() { |
24 | | - // Fetch data from an API |
25 | | - const res = await fetch('https://jsonplaceholder.typicode.com/users'); |
26 | | - const users = await res.json(); |
27 | | - |
28 | | - return ( |
29 | | - <div className="p-4"> |
30 | | - <h1 className="text-2xl font-bold">Users</h1> |
31 | | - <ul className="mt-4"> |
32 | | - {/* Render user data */} |
33 | | - </ul> |
34 | | - </div> |
35 | | - ); |
36 | | -} |
37 | | -``` |
38 | | -- **Hint**: Use `map()` to display user names. |
| 12 | +### **1. What is SSR?** |
| 13 | +Server-Side Rendering (SSR) means generating the HTML for a page on the server at request time. In Next.js, you achieve this using the `getServerSideProps` function. |
39 | 14 |
|
40 | 15 | --- |
41 | 16 |
|
42 | | -### **2. Static Site Generation (SSG)** |
43 | | -SSG generates static HTML at build time, ideal for data that doesn’t change frequently. |
| 17 | +### **2. Create a Page with SSR** |
| 18 | +- Add a new route: `src/app/ssr-example/page.tsx`. |
| 19 | +- Fetch data using `getServerSideProps`. |
44 | 20 |
|
45 | | -#### **Task: Create a Blog Page** |
46 | | -- Add a file: `src/app/blog/page.tsx`. |
47 | | -- Use `getStaticProps` to fetch blog post data. |
48 | | -- Display the blog titles in a list. |
49 | | - |
50 | | -##### Code Hint: |
| 21 | +##### Example (not complete): |
51 | 22 | ```tsx |
52 | | -// src/app/blog/page.tsx |
53 | | -export default async function BlogPage() { |
54 | | - // Fetch blog posts |
55 | | - const res = await fetch('https://jsonplaceholder.typicode.com/posts'); |
56 | | - const posts = await res.json(); |
| 23 | +// src/app/ssr-example/page.tsx |
| 24 | +export default async function SSRPage() { |
| 25 | + const data = await fetch('https://jsonplaceholder.typicode.com/posts/1').then(res => res.json()); |
57 | 26 |
|
58 | 27 | return ( |
59 | 28 | <div className="p-4"> |
60 | | - <h1 className="text-2xl font-bold">Blog Posts</h1> |
61 | | - <ul className="mt-4"> |
62 | | - {/* Render blog posts */} |
63 | | - </ul> |
| 29 | + <h1 className="text-2xl font-bold">Server-Side Rendered Data</h1> |
| 30 | + {/* Display fetched data here */} |
64 | 31 | </div> |
65 | 32 | ); |
66 | 33 | } |
67 | 34 | ``` |
68 | 35 |
|
69 | 36 | --- |
70 | 37 |
|
71 | | -### **3. Incremental Static Regeneration (ISR)** |
72 | | -ISR allows you to update static pages at runtime without rebuilding the entire app. |
73 | | - |
74 | | -#### **Task: Enable ISR** |
75 | | -- In your blog page, set a `revalidate` property for ISR. |
76 | | - |
77 | | -##### Code Hint: |
78 | | -```tsx |
79 | | -// src/app/blog/page.tsx |
80 | | -export async function generateStaticParams() { |
81 | | - return { revalidate: 60 }; // Revalidate every 60 seconds |
82 | | -} |
83 | | -``` |
| 38 | +### **3. Customize the Page** |
| 39 | +- Fetch and display additional data. |
| 40 | +- Style the page with Tailwind CSS. |
| 41 | +- Display a loading indicator or placeholder for better user experience. |
84 | 42 |
|
85 | 43 | --- |
86 | 44 |
|
87 | | -### **4. Error Handling** |
88 | | -- Handle errors in both `getServerSideProps` and `getStaticProps`. |
89 | | -- **Hint**: Wrap your `fetch` call in `try-catch`. |
| 45 | +### **4. Add a Link in the Navbar** |
| 46 | +- Update the `Navbar` component to include a link to the SSR example. |
90 | 47 |
|
91 | | -##### Example Error Handling: |
| 48 | +##### Example: |
92 | 49 | ```tsx |
93 | | -try { |
94 | | - const res = await fetch('https://jsonplaceholder.typicode.com/users'); |
95 | | - if (!res.ok) throw new Error('Failed to fetch data'); |
96 | | -} catch (error) { |
97 | | - console.error(error); |
98 | | -} |
| 50 | +<li> |
| 51 | + <Link href="/ssr-example">SSR Example</Link> |
| 52 | +</li> |
99 | 53 | ``` |
100 | 54 |
|
101 | 55 | --- |
102 | 56 |
|
103 | 57 | ## **Hints and Resources** |
| 58 | +### Common Challenges |
| 59 | +- **Data Not Displaying?**: Check the API URL and console for errors. |
| 60 | +- **Styling Issues?**: Ensure Tailwind CSS classes are applied correctly. |
104 | 61 |
|
105 | | -### Common Beginner Challenges |
106 | | -- **Data Not Displaying?**: Confirm the API endpoint is correct. |
107 | | -- **Slow Fetching?**: Check network logs for errors. |
108 | | -- **Empty Page?**: Ensure the fetch response is parsed using `.json()`. |
| 62 | +### Resources |
| 63 | +- [Next.js SSR Documentation](https://nextjs.org/docs/basic-features/data-fetching/get-server-side-props) |
| 64 | +- [JSONPlaceholder API](https://jsonplaceholder.typicode.com/) |
| 65 | + |
| 66 | +--- |
109 | 67 |
|
110 | | -### Helpful Resources |
111 | | -- [Next.js Data Fetching Documentation](https://nextjs.org/docs/data-fetching) |
112 | | -- [JSONPlaceholder Mock API](https://jsonplaceholder.typicode.com/) |
113 | | -- [Understanding SSR and SSG](https://nextjs.org/docs/basic-features/pages) |
| 68 | +## **Checklist** |
| 69 | +- [ ] Create an SSR page that fetches data. |
| 70 | +- [ ] Display fetched data on the page. |
| 71 | +- [ ] Add a link to the SSR page in the `Navbar`. |
114 | 72 |
|
115 | 73 | --- |
116 | 74 |
|
117 | | -## **Assignment Checklist** |
118 | | -- [ ] Create a Users Page with SSR. |
119 | | -- [ ] Create a Blog Page with SSG. |
120 | | -- [ ] Enable ISR for the Blog Page. |
121 | | -- [ ] Handle errors during data fetching. |
122 | | -- [ ] Verify that the data updates dynamically (for ISR). |
| 75 | +[← Previous Assignment](Assignment-1.md) | [Main README](../../README.md) | [Next Assignment →](Assignment-3.md) |
0 commit comments