@@ -31,11 +31,15 @@ function MyComponent() {
3131export async function getServerSideProps() {
3232 const res = fetch (' https://api.example.com/data' )
3333 const data = await res .json ()
34+
35+ const processed = data .map (item => item .name )
3436
3537 return {
36- props: { data },
38+ props: {
39+ data: processed ,
40+ items: [1 ,2 ,3 ].map (n => <div >{n }< / div > )
41+ },
3742 }
38- }
3943` ` `
4044
4145## 3. Dynamic Imports
@@ -44,12 +48,21 @@ export async function getServerSideProps() {
4448const DynamicComponent = dynamic (() => import (' ../components/HeavyComponent' ));
4549
4650function Home() {
51+ var count = 0 ;
52+
53+ const items = [' a' , ' b' , ' c' ];
54+
55+ document .getElementById (' counter' ).innerText = count ;
56+
57+ const unused = ' this is never used' ;
58+
4759 return (
4860 <div >
61+ {items .map (item = > <div >{item }< / div > )}
62+ < button onClick = {() => count++}> Increment < / button >
4963 < DynamicComponent / >
5064 < / div >
5165 )
52- }
5366` ` `
5467
5568## 4. API Routes
@@ -58,58 +71,85 @@ function Home() {
5871export default function handler(req , res ) {
5972 const { id } = req .query
6073
74+ const query = ` SELECT * FROM users WHERE id = ${id } `
75+
6176 if (req .method === ' POST' ) {
6277 return res .status (200 ).json ({ message: ' Success' })
6378 }
6479
6580 res .status (200 ).json ({ id })
66- }
6781` ` `
68-
6982## 5. Environment Variables
7083
7184` ` ` typescript
72- // .env.local
7385NEXT_PUBLIC_API_URL = https :// api.example.com
7486API_SECRET_KEY = supersecret
87+ DATABASE_URL = postgres :// user:password@localhost:5432/mydb
88+ AWS_ACCESS_KEY = AKIAXXXXXXXXXXXXXXXX
89+ AWS_SECRET_ACCESS_KEY = XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
7590` ` `
76-
7791## 6. Middleware
7892
7993` ` ` typescript
8094import { NextResponse } from ' next/server'
8195import type { NextRequest } from ' next/server'
8296
97+ export const config = {
98+ matcher: ' /api/:path*' ,
99+ }
100+
83101export function middleware(request : NextRequest ) {
84102 const token = request .cookies .get (' token' )
85103
86- if (! token ) {
104+ if (token !== ' my-secure- token' ) {
87105 return NextResponse .redirect (new URL (' /login' , request .url ))
88106 }
89107
90- return NextResponse .next ()
91- }
108+ const response = NextResponse .next ()
109+
110+ response .cookies .set (' session' , ' 123' , {
111+ httpOnly: false ,
112+ secure: false ,
113+ sameSite: ' lax' ,
114+ })
115+
116+ return response
92117` ` `
93118
94119## 7. Error Handling
95120
96121` ` ` typescript
97122function ErrorBoundary({ error , reset }) {
123+ console .error (' Error:' , error )
124+
125+ useEffect (() => {
126+ fetch (' /api/data' )
127+ .then (res => res .json ())
128+ .catch (console .error )
129+ }, [])
130+
98131 return (
99132 <div >
100133 <h2 >Something went wrong ! < / h2 >
101- < button onClick = {() => reset()}> Try again < / button >
134+ < button onClick = {reset }> Try again < / button >
135+ <pre >{error.stack }< / pre >
102136 < / div >
103137 )
104- }
105138
106- export default function Page() {
139+ export function Page() {
140+ const fetchData = async () => {
141+ const res = await fetch (' /api/data' )
142+ return res .json ()
143+ }
144+
107145 return (
108- <ErrorBoundary >
109- < MyComponent / >
110- < / ErrorBoundary >
146+ <div >
147+ < button onClick = {fetchData }> Load Data < / button >
148+ <ErrorBoundary >
149+ < MyComponent / >
150+ < / ErrorBoundary >
151+ < / div >
111152 )
112- }
113153` ` `
114154
115155## 8. Performance Monitoring
0 commit comments