withastro/astro · error · Error

context.rewrite is not available in Astro.

Error message

context.rewrite is not available in Astro.

What it means

Netlify's context.rewrite re-dispatches the request to another function/URL server-side. The Astro adapter does not bridge that mechanism, so the dev Context implements rewrite() as a method that always throws, telling you to use Astro's own rewrite/redirect facilities.

Solutions

  1. Use Astro.rewrite('/other-path') (Astro core API) for internal re-dispatch
  2. Use Astro.redirect(url, status) when a client redirect is acceptable
  3. Do routing-level fallbacks in middleware with context.redirect/context.rewrite from Astro, never the Netlify Context

Example fix

// before
return context.rewrite('/fallback');
// after
return Astro.rewrite('/fallback');
Defensive patterns

Strategy: fallback

Validate before calling

// Replace Netlify rewrites with Astro's own APIs up front:
if (needsFallback) {
  return Astro.rewrite('/fallback'); // or Astro.redirect('/fallback', 302)
}

Type guard

function isNetlifyRewriteAvailable(context: any): boolean {
  try {
    return typeof context?.rewrite === 'function' && context.deploy?.context !== 'dev';
  } catch {
    return false;
  }
}

Try / catch

try {
  return context.rewrite('/other');
} catch {
  return Astro.rewrite('/other');
}

Prevention

When it happens

Trigger: Calling context.rewrite('/other') in a page, endpoint, or middleware while running under the Netlify adapter in local dev.

Common situations: Functions code ported from Netlify that used rewrite for A/B routing or fallback pages; attempting rewrites inside on-demand rendered routes.

Related errors


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

Appendix: 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 3578d45d34)