-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy path104-nextjs-backend.mdc
More file actions
59 lines (43 loc) · 1.1 KB
/
104-nextjs-backend.mdc
File metadata and controls
59 lines (43 loc) · 1.1 KB
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
55
56
57
58
---
description: EXPLAIN how NextJS 15 backend methods work.
globs: *.ts, *.tsx
---
## Context
* NextJS 15 give us backend methods we should use.
## Header
Enable you to get the header of the request.
```tsx
import { headers } from 'next/headers'
export default async function Page() {
const headersList = await headers()
const userAgent = headersList.get('user-agent')
}
```
## Cookie
Enable you to get the cookie of the request.
```tsx
import { cookies } from 'next/headers'
export default async function Page() {
const cookieStore = await cookies()
const theme = cookieStore.get('theme')
return '...'
}
```
To set a cookie, you must be in an [api-route.mdc](mdc:.cursor/rules/api-route.mdc) or [server-actions.mdc](mdc:.cursor/rules/server-actions.mdc) :
```tsx
'use server'
import { cookies } from 'next/headers'
export async function create(data) {
const cookieStore = await cookies()
cookieStore.set('name', 'lee')
// or
cookieStore.set('name', 'lee', { secure: true })
// or
cookieStore.set({
name: 'name',
value: 'lee',
httpOnly: true,
path: '/',
})
}
```