You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
Copy file name to clipboardExpand all lines: docs/local-api/server-functions.mdx
+12-14
Original file line number
Diff line number
Diff line change
@@ -6,22 +6,20 @@ desc: Learn to use Local API operations with Server Functions in Payload to mana
6
6
keywords: server functions, local API, Payload, CMS, server-side logic, custom workflows, data management, headless CMS, TypeScript, Node.js, backend
7
7
---
8
8
9
-
# **Using Local API Operations with Server Functions**
10
-
11
9
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.
12
10
13
-
### **Why Use Server Functions?**
11
+
### Why Use Server Functions?
14
12
15
13
-**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.
16
14
-**Security Benefits**: Instead of exposing a full REST or GraphQL API, server functions restrict access to only the necessary operations, reducing potential security risks.
17
15
-**Performance Optimizations**: Next.js handles server functions efficiently, offering benefits like caching, optimized database queries, and reduced network overhead compared to traditional API calls.
18
16
-**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.
19
17
20
-
### **When to Use Server Functions**
18
+
### When to Use Server Functions
21
19
22
20
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.
23
21
24
-
## **Examples: Using Local API from Server Functions**
22
+
## Examples: Using Local API from Server Functions
25
23
26
24
All Local API operations can be used within server functions, allowing you to interact with Payload's backend securely.
27
25
@@ -34,7 +32,7 @@ In the following examples, we'll cover some common use cases, including:
34
32
- Handling file uploads when creating or updating a document
35
33
- Authenticating a user
36
34
37
-
### **Creating a Document**
35
+
### Creating a Document
38
36
39
37
First, let's create our server function. Here are some key points for this process:
When using server functions, proper error handling is essential to prevent unhandled exceptions and provide meaningful feedback to the frontend.
320
318
321
-
### **Best Practices**
319
+
### Best Practices#error-handling-best-practices
322
320
323
321
-**Wrap Local API calls in try/catch blocks** to catch potential errors.
324
322
-**Log errors on the server** for debugging purposes.
@@ -338,11 +336,11 @@ export async function createPost(data) {
338
336
}
339
337
```
340
338
341
-
## **Security Considerations**
339
+
## Security Considerations
342
340
343
341
Using server functions helps prevent direct exposure of Local API operations to the frontend, but additional security best practices should be followed:
344
342
345
-
### **Best Practices**
343
+
### Best Practices#security-best-practices
346
344
347
345
1.**Restrict access**: Ensure that sensitive actions (like user management) are only callable by authorized users.
348
346
2.**Avoid passing sensitive data**: Do not return sensitive information such as user data, passwords, etc.
0 commit comments