We've added the cookies
instance to the
GetServerSidePropsContext
, and also extended the request and response objects
with cookie handlers. Which one to choose depends on your personal style
preference. They offer the same functionality.
#context.cookies
The simplest way to use cookies, is probably via the context.cookies
namespace.
export const getServerSideProps = handle({ async get({ cookies }) { const session = cookies.get('session'); cookies.set('session', Date.now(), { expires: 300 }); }, });
See pillarjs/cookies for more options.
Alternatively, you might want to use the getCookie
and setCookie
helpers on
the request and response properties.
#req.getCookie
Extract a cookie with the given name from the Cookie header in the request.
export const getServerSideProps = handle({ async get({ req }) { const session = req.getCookie('session'); }, });
See pillarjs/cookies#cookies-get for more options.
#res.setCookie
This sets the given cookie in the response. If the value is omitted, an response header with an expired data is used to delete the cookie.
export const getServerSideProps = handle({ async get({ res }) { res.setCookie('session', Date.now(), { expires: 300 }); }, });
See pillarjs/cookies#cookies-set for more options.