-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy path107-server-components.mdc
54 lines (42 loc) · 1.29 KB
/
107-server-components.mdc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
---
description: Create and understand Server Components in NextJS 15
globs: *.tsx
---
## Context
* Since React 19 and NextJS 13 Server Component can be used.
* Server Component enable use to make backend query directly on our Component
* Server Component NEVER run on the client
## Usage
Utilize React 19 with Server Components. Implement Prisma queries and backend logic inside `page` or `layout` files like this:
```tsx
// Use "async" for server components
export default async function Page() {
// Use "await" for async operations
const result = await prisma.user.findMany();
return (
<div>
{result.map((user) => (
<p>{user.name}</p>
))}
</div>
);
}
```
You can also implement this logic in every component that is `async`. A Server Components can do backend stuff inside is body like :
* prisma query
* fs read / write
* analytics
* third partie stuff
Some method are avaiable :
```tsx
import { redirect } from 'next/navigation'
import { notFound } from 'next/navigation'
// Redirect to another page
redirect('/login');
// Show the `not-found.tsx` file
notFound()
```
Some rules about Server Components :
1. Server components are always `async`
2. Server components can't use hooks
3. Server Components can't use `document` or `window` because they are only run in backend