withastro/astro · error · Error

context.rewrite is not available in Astro.

Error message

context.rewrite is not available in Astro.

What it means

In the dev mock context, `rewrite` is a function that always throws `'context.rewrite is not available in Astro.'`. Astro performs rewrites via `Astro.redirect` / returning a `Response` with the rewritten target, or `return new Request(...)`. Netlify's `context.rewrite()` API is not bridged in the Astro adapter.

Source

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

				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[];

	return {
		name: '@astrojs/netlify',
		hooks: {
			'astro:config:setup': async ({ config, updateConfig, logger, command }) => {
				rootDir = config.root;
				await cleanFunctions();

				outDir = new URL(config.outDir, rootDir);

				let session = config.session;

View on GitHub (pinned to d081033d5f)

Solutions

  1. Use `Astro.redirect(to)` for HTTP redirects.
  2. For internal rewrites, return a `Response` or `Request` from the handler, or use `Astro.rewrite` if available in your Astro version.
  3. Use `_redirects` / `netlify.toml` redirects for path-level rewrites handled at the edge.

Example fix

// before
return context.rewrite('/new-path');

// after
return Astro.redirect('/new-path');
Defensive patterns

Strategy: fallback

Validate before calling

function rewriteOrRedirect(Astro: any, to: string) {
  if (typeof (Astro as any).rewrite === 'function') return (Astro as any).rewrite(to);
  return Astro.redirect(to);
}

Type guard

function hasAstroRewrite(Astro: any): Astro is { rewrite: (input: string | URL | Request) => Response } {
  return typeof Astro?.rewrite === 'function';
}

Prevention

When it happens

Trigger: Calling `context.rewrite(url)` in an Astro handler or middleware in dev. Reusing Netlify Edge Function code that performs rewrites via `context.rewrite`.

Common situations: Porting Netlify rewrite logic into Astro. Middleware that rewrites instead of redirecting through Astro's API.

Related errors


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