data
這是一個與 單次提取 (Single Fetch) 一起使用的工具,用於返回帶有狀態碼或自訂回應標頭的原始資料。這避免了需要將您的資料序列化為 Response
實例來提供自訂狀態/標頭。這通常取代了在單次提取之前使用 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" };
};