Skip to content

Commit d19412f

Browse files
authored
docs: adjust formatting on Local API - Server Functions documentation (#12058)
### What? Adjusts markdown formatting on Local API - Server Functions documentation ### Why? Some unnecessary characters and duplicate headline values causing issues on website frontend. ### How? Removes unnecessary characters and adds unique anchor tags for duplicate headlines.
1 parent bd557a9 commit d19412f

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

docs/local-api/server-functions.mdx

+12-14
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,20 @@ desc: Learn to use Local API operations with Server Functions in Payload to mana
66
keywords: server functions, local API, Payload, CMS, server-side logic, custom workflows, data management, headless CMS, TypeScript, Node.js, backend
77
---
88

9-
# **Using Local API Operations with Server Functions**
10-
119
In Next.js, **server functions** (previously called **server actions**) are special functions that run exclusively on the server, enabling secure backend logic execution while being callable from the frontend. These functions bridge the gap between client and server, allowing frontend components to perform backend operations without exposing sensitive logic.
1210

13-
### **Why Use Server Functions?**
11+
### Why Use Server Functions?
1412

1513
- **Executing Backend Logic from the Frontend**: The Local API is designed for server environments and cannot be directly accessed from client-side code. Server functions enable frontend components to trigger backend operations securely.
1614
- **Security Benefits**: Instead of exposing a full REST or GraphQL API, server functions restrict access to only the necessary operations, reducing potential security risks.
1715
- **Performance Optimizations**: Next.js handles server functions efficiently, offering benefits like caching, optimized database queries, and reduced network overhead compared to traditional API calls.
1816
- **Simplified Development Workflow**: Rather than setting up full API routes with authentication and authorization checks, server functions allow for lightweight, direct execution of necessary operations.
1917

20-
### **When to Use Server Functions**
18+
### When to Use Server Functions
2119

2220
Use server functions whenever you need to call Local API operations from the frontend. Since the Local API is only accessible from the backend, server functions act as a secure bridge, eliminating the need to expose additional API endpoints.
2321

24-
## **Examples: Using Local API from Server Functions**
22+
## Examples: Using Local API from Server Functions
2523

2624
All Local API operations can be used within server functions, allowing you to interact with Payload's backend securely.
2725

@@ -34,7 +32,7 @@ In the following examples, we'll cover some common use cases, including:
3432
- Handling file uploads when creating or updating a document
3533
- Authenticating a user
3634

37-
### **Creating a Document**
35+
### Creating a Document
3836

3937
First, let's create our server function. Here are some key points for this process:
4038

@@ -95,7 +93,7 @@ export const PostForm: React.FC = () => {
9593
};
9694
```
9795

98-
### **Updating a Document**
96+
### Updating a Document
9997

10098
The key points from the previous example also apply here.
10199

@@ -156,7 +154,7 @@ export const UpdatePostForm: React.FC = () => {
156154

157155
```
158156

159-
### **Authenticating a User**
157+
### Authenticating a User
160158

161159
In this example, we will check if a user is authenticated using Payload's authentication system. Here's how it works:
162160

@@ -218,7 +216,7 @@ export const AuthComponent: React.FC = () => {
218216
};
219217
```
220218

221-
### **Creating a Document with File Upload**
219+
### Creating a Document with File Upload
222220

223221
This example demonstrates how to write a server function that creates a document with a file upload. Here are the key steps:
224222

@@ -310,15 +308,15 @@ export const PostForm: React.FC = () => {
310308
};
311309
```
312310

313-
## **Reusable Payload Server Functions**
311+
## Reusable Payload Server Functions
314312

315313
Coming soon…
316314

317-
## **Error Handling in Server Functions**
315+
## Error Handling in Server Functions
318316

319317
When using server functions, proper error handling is essential to prevent unhandled exceptions and provide meaningful feedback to the frontend.
320318

321-
### **Best Practices**
319+
### Best Practices#error-handling-best-practices
322320

323321
- **Wrap Local API calls in try/catch blocks** to catch potential errors.
324322
- **Log errors on the server** for debugging purposes.
@@ -338,11 +336,11 @@ export async function createPost(data) {
338336
}
339337
```
340338

341-
## **Security Considerations**
339+
## Security Considerations
342340

343341
Using server functions helps prevent direct exposure of Local API operations to the frontend, but additional security best practices should be followed:
344342

345-
### **Best Practices**
343+
### Best Practices#security-best-practices
346344

347345
1. **Restrict access**: Ensure that sensitive actions (like user management) are only callable by authorized users.
348346
2. **Avoid passing sensitive data**: Do not return sensitive information such as user data, passwords, etc.

0 commit comments

Comments
 (0)