跳转到内容

defer

defer

要开始使用流数据,请查看流指南

这是创建流式/延迟响应的快捷方式。它假定您使用的是 utf-8 编码。从开发人员的角度来看,它的行为就像 json(),但能够将承诺传输到您的 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",
},
}
);
};