Cookie not set in pages deployed, but working with local wrangler

Hi Guys, I see that there seem to be issue with setting cookies for nextJS pages on cloudflare. Can any kind soul provide me with the reference on how to allow cookie to be set on the deployed pages? Below is a snippet of my code to set the cookie:
const CUSTOM_COOKIE = 'x-custom-cookie';
export async function GET(request: NextRequest) {
response.cookies.set(CUSTOM_COOKIE, 'cookie_value', {
path: '/', // Cookie available on all paths
httpOnly: true, // Cannot be accessed via JavaScript
secure: true, // Only sent over HTTPS
maxAge: 60 * 120 // client-side cookie age (2 hours)
});
return response;
}
const CUSTOM_COOKIE = 'x-custom-cookie';
export async function GET(request: NextRequest) {
response.cookies.set(CUSTOM_COOKIE, 'cookie_value', {
path: '/', // Cookie available on all paths
httpOnly: true, // Cannot be accessed via JavaScript
secure: true, // Only sent over HTTPS
maxAge: 60 * 120 // client-side cookie age (2 hours)
});
return response;
}
1 Reply
hanbaobao
hanbaobaoOP7mo ago
Ok, this issue has been resolved. It seems like there was a confusion between preview and production. The relevant secrets were not set for preview build and therefore the cookie wasn't generated. Both response.cookies.set() and headers.append() works in setting the cookies

Did you find this page helpful?