#Netlify set cookie on edge

4 messages · Page 1 of 1 (latest)

vagrant iris
#

I am using the netlify adapter and trying to set a cookie on edge.

This test code is not working, cookie is never set:

import { defineMiddleware } from 'astro:middleware';

export const onRequest = defineMiddleware(async (context, next) => {
console.log('[Debug] Incoming request:', context.request.url);

context.cookies.set('test_cookie', 'hello-world', {
path: '/',
httpOnly: true,
sameSite: 'strict'
});
console.log('[Debug] test_cookie set');

console.log('[Debug] Passing to next middleware');
return next();
});

wanton birchBOT
#

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

#

You’re running this in middleware on Netlify Edge. There are two key limitations in the docs that explain why your cookie isn’t being set:

  1. You can only modify headers/cookies at the page/endpoint (response) level, not inside components or after streaming has started. [On‑demand features; Response error]
    Middleware runs before the page/endpoint is rendered, but on Netlify with edgeMiddleware: true your middleware code is actually executing in a Netlify Edge Function that then forwards the request to the serverless function which sends the final response. [Netlify usage]

  2. On Netlify, middleware is primarily for controlling/annotating the request (locals, rewrites, access‑control), not for directly shaping the final response headers from the edge. When edgeMiddleware is enabled, Astro serializes context.locals into a header and the serverless function does the actual rendering and response. [Netlify usage; Middleware guide]

#

The knowledge sources do not explicitly document context.cookies behavior in edge middleware, nor do they show a working example of setting cookies from edge middleware on Netlify. They only guarantee cookie support for pages and API endpoints rendered on demand (using Astro.cookies / context.cookies in those handlers). [Cookies API; On‑demand cookies]

Given this, the reliable approach documented is:

  • Set cookies in the page or API route itself, not in edge middleware:
// Example: src/pages/index.astro
---
export const prerender = false; // or output: 'server'

let counter = 0;
if (Astro.cookies.has('counter')) {
  const cookie = Astro.cookies.get('counter');
  const value = cookie?.number();
  if (value !== undefined && !isNaN(value)) counter = value + 1;
}
Astro.cookies.set('counter', String(counter), {
  path: '/',
  httpOnly: true,
  sameSite: 'strict',
});
---
<h1>Counter = {counter}</h1>

[On‑demand cookies]

You can still use Netlify edge middleware to add request metadata into context.locals (or Astro.locals later) and then make the final decision about which cookies to set in the page/endpoint handler.

Because the docs do not describe cookie support from Netlify edge middleware specifically, I can’t confidently provide a working edge‑only snippet; the documented and supported path is to set cookies from the server-rendered page or API route.

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