Home / Function/ actionCookieForwarding() — astro Function Reference

actionCookieForwarding() — astro Function Reference

Architecture documentation for the actionCookieForwarding() function in middleware.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  c2dd9f41_acfa_28a2_c292_ca8723f112fc["actionCookieForwarding()"]
  947df197_61f5_6433_3704_d6055d393fa8["middleware.ts"]
  c2dd9f41_acfa_28a2_c292_ca8723f112fc -->|defined in| 947df197_61f5_6433_3704_d6055d393fa8
  style c2dd9f41_acfa_28a2_c292_ca8723f112fc fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro/test/fixtures/actions/src/middleware.ts lines 4–44

const actionCookieForwarding = defineMiddleware(async (ctx, next) => {
	if (ctx.isPrerendered) return next();

	const { action, setActionResult, serializeActionResult } = getActionContext(ctx);

	const payload = ctx.cookies.get('ACTION_PAYLOAD');
	if (payload) {
		const { actionName, actionResult } = payload.json();
		setActionResult(actionName, actionResult);
		ctx.cookies.delete('ACTION_PAYLOAD');
		return next();
	}

	if (
		action?.calledFrom === 'rpc' &&
		action.name === 'locked' &&
		!ctx.cookies.has('actionCookie')
	) {
		return new Response('Unauthorized', { status: 401 });
	}

	if (action?.calledFrom === 'form' && ctx.url.searchParams.has('actionCookieForwarding')) {
		const actionResult = await action.handler();

		ctx.cookies.set('ACTION_PAYLOAD', {
			actionName: action.name,
			actionResult: serializeActionResult(actionResult),
		});

		if (actionResult.error) {
			const referer = ctx.request.headers.get('Referer');
			if (!referer) {
				throw new Error('Internal: Referer unexpectedly missing from Action POST request.');
			}
			return ctx.redirect(referer);
		}
		return ctx.redirect(ctx.originPathname);
	}

	return next();
});

Subdomains

Frequently Asked Questions

What does actionCookieForwarding() do?
actionCookieForwarding() is a function in the astro codebase, defined in packages/astro/test/fixtures/actions/src/middleware.ts.
Where is actionCookieForwarding() defined?
actionCookieForwarding() is defined in packages/astro/test/fixtures/actions/src/middleware.ts at line 4.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free