Home / File/ middleware.js — astro Source File

middleware.js — astro Source File

Architecture documentation for middleware.js, a javascript file in the astro codebase. 1 imports, 0 dependents.

Entity Profile

Dependency Diagram

graph LR
  78d5cc03_20df_63a2_afe4_c263dd05734a["middleware.js"]
  3a7a7ff7_6c64_95f9_af85_4deb518834f1["astro:middleware"]
  78d5cc03_20df_63a2_afe4_c263dd05734a --> 3a7a7ff7_6c64_95f9_af85_4deb518834f1
  style 78d5cc03_20df_63a2_afe4_c263dd05734a fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import { defineMiddleware, sequence } from 'astro:middleware';

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();
});

const second = defineMiddleware(async (context, next) => {
	if (context.request.url.includes('/second')) {
		context.locals.name = 'second';
	} else if (context.request.url.includes('/redirect')) {
		return context.redirect('/', 302);
	}
	return await next();
});

const third = defineMiddleware(async (context, next) => {
	if (context.request.url.includes('/broken-locals')) {
		Object.assign(context.locals, {
			fn() {},
		});
	} else if (context.request.url.includes('/does-nothing')) {
		return undefined;
	}
	return next();
});

const fourth = defineMiddleware((context, next) => {
	if (context.request.url.includes('/no-route-but-200')) {
		return new Response("It's OK!", {
			status: 200
		});
	}
	return next()
})

export const onRequest = sequence(first, second, third, fourth);

Subdomains

Dependencies

  • astro:middleware

Frequently Asked Questions

What does middleware.js do?
middleware.js is a source file in the astro codebase, written in javascript. It belongs to the IntegrationAdapters domain, SsrAdapters subdomain.
What functions are defined in middleware.js?
middleware.js defines 4 function(s): first, fourth, second, third.
What does middleware.js depend on?
middleware.js imports 1 module(s): astro:middleware.
Where is middleware.js in the architecture?
middleware.js is located at packages/astro/test/fixtures/middleware space/src/middleware.js (domain: IntegrationAdapters, subdomain: SsrAdapters, directory: packages/astro/test/fixtures/middleware space/src).

Analyze Your Own Codebase

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

Try Supermodel Free