json

This is a shortcut for creating application/json responses. It assumes you are using utf-8 encoding.

  1. import { json } from "@remix-run/node"; // or cloudflare/deno
  2. export const loader = async () => {
  3. // So you can write this:
  4. return json({ any: "thing" });
  5. // Instead of this:
  6. return new Response(JSON.stringify({ any: "thing" }), {
  7. headers: {
  8. "Content-Type": "application/json; charset=utf-8",
  9. },
  10. });
  11. };

You can also pass a status code and headers:

  1. export const loader = async () => {
  2. return json(
  3. { not: "coffee" },
  4. {
  5. status: 418,
  6. headers: {
  7. "Cache-Control": "no-store",
  8. },
  9. }
  10. );
  11. };