#Server actions custom headers

1 messages · Page 1 of 1 (latest)

remote sundial
#

Can I pass custom headers from the client with every server action? I can't use cookies in this context and I would like to avoid adding data to every action function call

plain trellisBOT
#

Hi @remote sundial. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Based on the information provided in the extracts, it's not explicitly mentioned whether you can pass custom headers from the client with every server action in Next.js. However, it's important to note that server actions in Next.js are designed to handle data fetching and mutations on the server side, and they inherently have access to the request headers.

If you need to set custom headers, you can do so in the next.config.js file for Next.js projects or in a Serverless Function, as mentioned in the Vercel documentation (https://vercel.com/docs/edge-network/headers). However, this applies to the response headers and not the request headers.

For passing data to server actions, the common practice is to pass them as arguments to the server action function calls. If you can't use cookies and want to avoid adding data to every action function call, you might need to reconsider your application design or look for alternative ways to pass the data.

Remember that manipulating headers can have implications on the behavior of your application and should be done with caution. Always refer to the official Next.js and Vercel documentation for the most accurate and up-to-date information.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@remote sundial kapa.ai is still learning and improving, please let us know how it did by reacting below

remote sundial
#

@plain trellis is the cache function from 'react' isolated per request in nextjs?

plain trellisBOT
#

Let me check the knowledge base for you. One moment! :books:

#

Yes, the cache function from 'react' is isolated per request in Next.js. This is part of the Request Memoization feature, which is a React feature, not a Next.js feature. It's included to show how it interacts with the other caching mechanisms in Next.js.

The cache function allows you to memoize the return value of a function, allowing you to call the same function multiple times while only executing it once. This is particularly useful for data fetching operations where the fetch API is not suitable, such as some database clients, CMS clients, or GraphQL clients.

Here's an example of how you can use the cache function:

import { cache } from 'react'
import db from '@/lib/db'
 
export const getItem = cache(async (id: string) => {
 const item = await db.item.findUnique({ id })
 return item
})

In this example, the getItem function is memoized using the cache function. This means that even if getItem is called multiple times with the same id during the same request, it will only execute once, and the result will be reused for subsequent calls.

However, it's important to note that this memoization only lasts the lifetime of a request. This means that the cache is not shared across different requests. Each request will have its own isolated cache.

For more information, you can refer to the Next.js documentation on Request Memoization (https://nextjs.org/docs/app/building-your-application/caching#request-memoization) and the React cache function (https://nextjs.org/docs/app/building-your-application/caching#react-cache-function).

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: