@@ -19,3 +19,104 @@ tags: [courses,beginner-level,FramWorks,Introduction]
19
19
- ** On-Demand Rendering** : Pages are generated on each request, fetching data and rendering HTML dynamically.
20
20
- ** Performance** : Slower compared to static rendering because the server must generate the HTML for each request.
21
21
- ** Use Cases** : Suitable for pages that require real-time data or frequent updates, such as dashboards, user profiles, and dynamic content.
22
+
23
+ ## Implementing Static Generation (` getStaticProps ` )
24
+
25
+ ` getStaticProps ` is a Next.js function that allows you to fetch data at build time to pre-render a page.
26
+
27
+ 1 . ** Creating a Page with ` getStaticProps ` ** :
28
+
29
+ ``` javascript title="pages/posts/[id].js"
30
+ import { useRouter } from " next/router" ;
31
+
32
+ export async function getStaticProps ({ params }) {
33
+ // Fetch data for the post using the id from params
34
+ const res = await fetch (
35
+ ` https://jsonplaceholder.typicode.com/posts/${ params .id } ` ,
36
+ );
37
+ const post = await res .json ();
38
+
39
+ return {
40
+ props: {
41
+ post,
42
+ },
43
+ };
44
+ }
45
+
46
+ export async function getStaticPaths () {
47
+ // Specify dynamic routes to pre-render based on data
48
+ const res = await fetch (" https://jsonplaceholder.typicode.com/posts" );
49
+ const posts = await res .json ();
50
+
51
+ const paths = posts .map ((post ) => ({
52
+ params: { id: post .id .toString () },
53
+ }));
54
+
55
+ return {
56
+ paths,
57
+ fallback: false ,
58
+ };
59
+ }
60
+
61
+ export default function Post ({ post }) {
62
+ const router = useRouter ();
63
+ if (router .isFallback ) {
64
+ return < div> Loading... < / div> ;
65
+ }
66
+
67
+ return (
68
+ < div>
69
+ < h1> {post .title }< / h1>
70
+ < p> {post .body }< / p>
71
+ < / div>
72
+ );
73
+ }
74
+ ```
75
+
76
+ 2 . ** Explanation** :
77
+
78
+ - ` getStaticProps ` fetches data at build time and passes it as props to the page component.
79
+ - ` getStaticPaths ` specifies the dynamic routes to be pre-rendered.
80
+ - ` fallback: false ` ensures that only the specified paths are generated at build time.
81
+ - ` router.isFallback ` checks if the page is in fallback mode and displays a loading indicator.
82
+ - The ` Post ` component displays the post title and body fetched from the API.
83
+
84
+ ## Implementing Server-Side Rendering (` getServerSideProps ` )
85
+
86
+ ` getServerSideProps ` is a Next.js function that allows you to fetch data on each request to pre-render a page.
87
+
88
+ 1 . ** Creating a Page with ` getServerSideProps ` ** :
89
+
90
+ ``` javascript title="pages/user/[id].js"
91
+ import { useRouter } from " next/router" ;
92
+
93
+ export async function getServerSideProps ({ params }) {
94
+ // Fetch user data using the id from params
95
+ const res = await fetch (
96
+ ` https://jsonplaceholder.typicode.com/users/${ params .id } ` ,
97
+ );
98
+ const user = await res .json ();
99
+
100
+ return {
101
+ props: {
102
+ user,
103
+ },
104
+ };
105
+ }
106
+
107
+ export default function User ({ user }) {
108
+ return (
109
+ < div>
110
+ < h1> {user .name }< / h1>
111
+ < p> Email: {user .email }< / p>
112
+ < / div>
113
+ );
114
+ }
115
+ ```
116
+
117
+ 2 . ** Explanation** :
118
+
119
+ - ` getServerSideProps ` fetches data on each request and passes it as props to the page component.
120
+ - The ` User ` component displays the user's name and email fetched from the API.
121
+
122
+ In this lesson, you learned about static and server-side rendering in Next.js and how to implement them using ` getStaticProps ` and ` getServerSideProps ` functions. Static rendering is ideal for content that does not change frequently, while server-side rendering is suitable for real-time data and dynamic content.
0 commit comments