withastro/astro · error · Error

Please use Astro.cookies instead.

Error message

Please use Astro.cookies instead.

What it means

Same mock-context pattern as the `url` getter: in dev, `context.cookies` is `get cookies(): never` and throws on access because Astro owns cookie access via `Astro.cookies`. Netlify's cookies API is intentionally unavailable inside Astro handlers to avoid double sources of truth.

Source

Thrown at packages/integrations/netlify/src/index.ts:596

				country: { code: 'mock', name: 'Mock Country' },
				subdivision: { code: 'SD', name: 'Mock Subdivision' },
				timezone: 'UTC',
				longitude: 0,
				latitude: 0,
			},
			ip:
				typeof req.headers['x-nf-client-connection-ip'] === 'string'
					? req.headers['x-nf-client-connection-ip']
					: (req.socket.remoteAddress ?? '127.0.0.1'),
			server: {
				region: 'local-dev',
			},
			requestId:
				typeof req.headers['x-nf-request-id'] === 'string'
					? req.headers['x-nf-request-id']
					: 'mock-netlify-request-id',
			get cookies(): never {
				throw new Error('Please use Astro.cookies instead.');
			},
			json: (input) => Response.json(input),
			log: console.info,
			next: () => {
				throw new Error('`context.next` is not implemented for serverless functions');
			},
			get params(): never {
				throw new Error("context.params don't contain any usable content in Astro.");
			},
			rewrite() {
				throw new Error('context.rewrite is not available in Astro.');
			},
		};

		return context;
	}

	let routes: IntegrationResolvedRoute[];

View on GitHub (pinned to d081033d5f)

Solutions

  1. Use `Astro.cookies.get(name)` / `Astro.cookies.set(name, value, options)` / `.delete(name)`.
  2. Remove direct reads of `context.cookies` in dev paths.
  3. If sharing code between Astro and plain Netlify Functions, branch on environment.

Example fix

// before
const token = context.cookies.get('token');

// after
const token = Astro.cookies.get('token')?.value;
Defensive patterns

Strategy: validation

Validate before calling

function getCookie(Astro: any, name: string) {
  return Astro.cookies.get(name)?.value;
}

Type guard

function hasAstroCookies(Astro: any): Astro is { cookies: { get: (n: string) => any } } {
  return !!Astro?.cookies && typeof Astro.cookies.get === 'function';
}

Prevention

When it happens

Trigger: Calling `context.cookies.get(...)` / `.set(...)` in an Astro endpoint/middleware in dev. Reused Netlify Function code that operated on `context.cookies`. Enumerating the context object in dev.

Common situations: Porting Netlify Function cookie logic into Astro. Debug code that iterates context keys.

Related errors


AI-assisted analysis of withastro/astro@d081033d5f (2026-08-12). Data as JSON: /api/errors/ec6a934013e66dbe. Report an issue: GitHub.