网页 综合 更新时间 2026-11-06 00:00:00

cookies

自动蜘蛛 nextjs.org 正文约 2115 字
cookies
cookies is an async function that allows you to read the HTTP incoming request cookies in Server Components, and read/write outgoing request cookies in Server Functions or Route Handlers.
The following methods are available:
When setting a cookie, the following properties from the options object are supported:
The only option with a default value is path.
To learn more about these options, see the MDN docs.
When working with cookies in Server Components, it's important to understand that cookies are fundamentally a client-side storage mechanism:
The server can only send instructions (via Set-Cookie headers) to tell the browser to store cookies - the actual storage happens on the client side. This is why cookie operations that modify state (.set, .delete) must be performed in a Server Function or Route Handler where the response headers can be properly set.
After you set or delete a cookie in a Server Function, Next.js can return both the updated UI and new data in a single server roundtrip when the function is used as a Server Action (e.g., passed to a form's action prop). See Caching and Revalidating.
The UI is not unmounted, but effects that depend on data coming from the server will re-run.
To refresh cached data too, call revalidatePath or revalidateTag inside the function.
You can use the (await cookies()).get('name') method to get a single cookie:
You can use the (await cookies()).getAll() method to get all cookies with a matching name. If name is unspecified, it returns all the available cookies.
You can use the (await cookies()).set(name, value, options) method in a Server Function or Route Handler to set a cookie. The options object is optional.
You can use the (await cookies()).has(name) method to check if a cookie exists:
There are three ways you can delete a cookie.
Using the delete() method:
Setting a new cookie with the same name and an empty value:
Setting the maxAge to 0 will immediately expire a cookie. maxAge accepts a value in seconds.
Last updated November 6, 2026
Subscribe to our newsletter
Stay updated on new releases and features, guides, and case studies.
访问原文 返回搜索 https://nextjs.org/docs/app/api-reference/functions/cookies
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。