data
这是一个与 单次获取 配合使用的实用程序,用于返回原始数据,并附带状态码或自定义响应头。这避免了将数据序列化为 Response
实例以提供自定义状态/标头的需要。这通常是在 Single Fetch 之前使用 json
或 defer
的 loader
/action
函数的替代品。
¥This is a utility for use with Single Fetch to return raw data accompanied by a status code or custom response headers. This avoids the need to serialize your data into a Response
instance to provide custom status/headers. This is generally a replacement for loader
/action
functions that used json
or defer
prior to Single Fetch.
import { data } from "@remix-run/node"; // or cloudflare/deno
export const loader = async () => {
return data(
{ not: "coffee" },
{
status: 418,
headers: {
"Cache-Control": "no-store",
},
}
);
};
如果你不需要返回自定义状态/标头,则不应使用此功能。 - 在这种情况下,只需直接返回数据:
¥You should not be using this function if you don't need to return custom status/headers - in that case, just return the data directly:
export const loader = async () => {
// ❌ Bad
return data({ not: "coffee" });
// ✅ Good
return { not: "coffee" };
};