Performance: Request parsing
Request parsing affects how quickly the app processes incoming data, impacting interaction responsiveness and server response time.
const data = await request.json();
const data = JSON.parse(await request.text());
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Manual JSON.parse on text | 0 | 0 | blocks main thread causing delayed render | [X] Bad |
| Using request.json() async method | 0 | 0 | non-blocking, allows smooth render | [OK] Good |
request.json() is the built-in method to parse JSON body content from a request.formData() to parse.await request.formData() returns a FormData object representing the form fields.export async function POST({ request, url }) {
const jsonData = await request.json();
const searchParam = url.searchParams.get('id');
return new Response(`ID: ${searchParam}, Name: ${jsonData.name}`);
}{"name":"Alice"} and the URL is /api?id=42?await request.json() to get { name: 'Alice' } from the request body.url.searchParams.get('id') reads the 'id' parameter from the URL, which is '42'.export async function POST({ request }) {
const data = request.formData();
const username = data.get('username');
return new Response(username);
}request.formData() returns a promise and must be awaited to get the FormData object.request.formData() without await, so data is a promise, not a FormData instance, causing an error on data.get().request.json(). Otherwise, it uses request.formData() and converts form entries to an object.Pass the test and this pattern moves unknown