defer
要開始使用串流資料,請查看串流指南。
這是建立串流/延遲回應的快捷方式。它假設您正在使用 utf-8
編碼。從開發人員的角度來看,它的行為就像 json()
一樣,但能夠將 Promise 傳輸到您的 UI 元件。
import { defer } from "@remix-run/node"; // or cloudflare/deno
export const loader = async () => {
const aStillRunningPromise = loadSlowDataAsync();
// So you can write this without awaiting the promise:
return defer({
critical: "data",
slowPromise: aStillRunningPromise,
});
};
您還可以傳遞狀態碼和標頭
export const loader = async () => {
const aStillRunningPromise = loadSlowDataAsync();
return defer(
{
critical: "data",
slowPromise: aStillRunningPromise,
},
{
status: 418,
headers: {
"Cache-Control": "no-store",
},
}
);
};