defer

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.

  1. import { defer } from "@remix-run/node"; // or cloudflare/deno
  2. export const loader = async () => {
  3. const aStillRunningPromise = loadSlowDataAsync();
  4. // So you can write this without awaiting the promise:
  5. return defer({
  6. critical: "data",
  7. slowPromise: aStillRunningPromise,
  8. });
  9. };

You can also pass a status code and headers:

  1. export const loader = async () => {
  2. const aStillRunningPromise = loadSlowDataAsync();
  3. return defer(
  4. {
  5. critical: "data",
  6. slowPromise: aStillRunningPromise,
  7. },
  8. {
  9. status: 418,
  10. headers: {
  11. "Cache-Control": "no-store",
  12. },
  13. }
  14. );
  15. };