Performance: Thrown responses for HTTP errors
This affects how quickly error responses are sent and rendered, impacting user experience during failures.
import { json } from '@remix-run/node'; import { Response } from '@remix-run/node'; export async function loader() { const data = await fetchSomeData(); if (!data) { throw new Response('Not Found', { status: 404 }); } return json(data); }
export async function loader() { const data = await fetchSomeData(); if (!data) { return new Response('Not Found', { status: 404 }); } return json(data); }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Returning Response manually | N/A | N/A | Delays error paint | [X] Bad |
| Throwing Response for errors | N/A | N/A | Fast error paint | [OK] Good |
throw new Response do in Remix when handling HTTP errors?throw new Responsethrow new Response("Not Found", { status: 404 });. Others have wrong order, missing 'new', or wrong parameters.throw new Response("Unauthorized", { status: 401 });throw new Response({ message: "Server Error" }, 500);Pass the test and this pattern moves unknown