Performance: Request and response handling
This affects how fast the server processes requests and sends responses, impacting page load speed and interactivity.
import { cache } from 'astro/cache'; export const GET = cache(async () => { const data = await fetch('https://api.example.com/data'); const json = await data.json(); return new Response(JSON.stringify(json)); });
export async function GET() { const data = await fetch('https://api.example.com/data'); const json = await data.json(); return new Response(JSON.stringify(json)); }
| Pattern | Server Processing | Network Calls | Response Time | Verdict |
|---|---|---|---|---|
| No caching, fetch on every request | High | Many repeated calls | Slow | [X] Bad |
| Cached fetch with async handling | Low | Single call cached | Fast | [OK] Good |
GET handles HTTP GET requests specifically.POST.export async function GET() {
return new Response(JSON.stringify({ message: 'Hello' }), {
headers: { 'Content-Type': 'application/json' }
});
}export async function POST(request) {
const data = await request.json();
return new Response('Received');
}request property, not the request directly.POST({ request }) to access the request object.request to access the request data.await request.json() to get the JSON data, then respond with a JSON string including the greeting.'Content-Type': 'application/json' for JSON response.Pass the test and this pattern moves unknown