data
data
这是一个与 Single Fetch 一起使用的实用程序,用于返回带有状态代码或自定义响应标头的原始数据。这样就无需将数据序列化为 Response
实例来提供自定义状态 / 标头。这通常是在 Single Fetch 之前使用 json
或 defer
的 loader
/action
函数的替代品。
import { data } from "@remix-run/node"; // or cloudflare/deno
export const loader = async () => { return data( { not: "coffee" }, { status: 418, headers: { "Cache-Control": "no-store", }, } );};
如果您不需要返回自定义状态 / 标题,则不应该使用此功能 - 在这种情况下,只需直接返回数据:
export const loader = async () => { // ❌ Bad return data({ not: "coffee" });
// ✅ Good return { not: "coffee" };};