defer
要开始使用流数据,请查看 Streaming 指南。
¥To get started with streaming data, check out the Streaming Guide.
这是创建流式/延迟响应的快捷方式。它假设你使用的是 utf-8
编码。从开发者的角度来看,它的行为与 json()
类似,但能够将 Promise 传递到你的 UI 组件。
¥This is a shortcut for creating a streaming/deferred response. It assumes you are using utf-8
encoding. From a developer perspective it behaves just like json()
, but with the ability to transport promises to your UI components.
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,
});
};
你还可以传递状态码和标头:
¥You can also pass a status code and headers:
export const loader = async () => {
const aStillRunningPromise = loadSlowDataAsync();
return defer(
{
critical: "data",
slowPromise: aStillRunningPromise,
},
{
status: 418,
headers: {
"Cache-Control": "no-store",
},
}
);
};