Home / Function/ first() — astro Function Reference

first() — astro Function Reference

Architecture documentation for the first() function in middleware.js from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  8bb96717_3c2b_9588_01ea_741cba01ce8f["first()"]
  78d5cc03_20df_63a2_afe4_c263dd05734a["middleware.js"]
  8bb96717_3c2b_9588_01ea_741cba01ce8f -->|defined in| 78d5cc03_20df_63a2_afe4_c263dd05734a
  style 8bb96717_3c2b_9588_01ea_741cba01ce8f fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro/test/fixtures/middleware space/src/middleware.js lines 3–65

const first = defineMiddleware(async (context, next) => {
	// Auth check: protect /admin route
	if (context.url.pathname === '/admin') {
		const authToken = context.request.headers.get('Authorization');
		if (!authToken) {
			return context.redirect('/');
		}
		// Auth token present, allow access
		return await next();
	}

	if (context.request.url.includes('/lorem')) {
		context.locals.name = 'ipsum';
	} else if (context.request.url.includes('/rewrite')) {
		return new Response('<span>New content!!</span>', {
			status: 200,
		});
	} else if (context.request.url.includes('/content-policy')) {
		const response = await next();
		response.headers.append('X-Content-Type-Options', 'nosniff');
		response.headers.append('Content-Type', 'application/json');

		return next();
	} else if (context.request.url.includes('/broken-500')) {
		return new Response(null, {
			status: 500,
		});
	} else if (context.request.url.includes('/api/endpoint')) {
		const response = await next();
		const object = await response.json();
		object.name = 'REDACTED';
		return new Response(JSON.stringify(object), {
			headers: response.headers,
		});
	} else if (context.url.pathname === '/throw') {
		throw new Error();
	} else if (context.url.pathname === '/clone') {
		const response = await next();
		const newResponse = response.clone();
		const /** @type {string} */ html = await newResponse.text();
		const newhtml = html.replace('testing', 'it works');
		return new Response(newhtml, { status: 200, headers: response.headers });
	} else if (context.url.pathname === '/return-response-cookies') {
		const response = await next();
		const html = await response.text();

		return new Response(html, {
			status: 200,
			headers: response.headers,
		});
	} else if (context.url.pathname === '/prerendered/') {
		context.locals.canBeReadDuringPrerendering = "yes they can!";
	} else {
		if (context.url.pathname === '/') {
			context.cookies.set('foo', 'bar');
		}

		Object.assign(context.locals, {
			name: 'bar',
		});
	}
	return await next();
});

Subdomains

Frequently Asked Questions

What does first() do?
first() is a function in the astro codebase, defined in packages/astro/test/fixtures/middleware space/src/middleware.js.
Where is first() defined?
first() is defined in packages/astro/test/fixtures/middleware space/src/middleware.js at line 3.

Analyze Your Own Codebase

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

Try Supermodel Free