redirectDocument

redirectDocument

这是一个围绕 redirect 的小封装器,它将触发文档级重定向到新位置,而不是客户端导航。

¥This is a small wrapper around redirect that will trigger a document-level redirect to the new location instead of a client-side navigation.

当 Remix 应用与非 Remix 应用位于同一域中,并且需要从 Remix 应用重定向到非 Remix 应用时,这最有用:

¥This is most useful when you have a Remix app living next to a non-Remix app on the same domain and need to redirect from the Remix app to the non-Remix app:

import { redirectDocument } from "@remix-run/node"; // or cloudflare/deno

export const action = async () => {
  const userSession = await getUserSessionOrWhatever();

  if (!userSession) {
    // Assuming `/login` is a separate non-Remix app
    return redirectDocument("/login");
  }

  return json({ ok: true });
};

与 目录中的 redirectResponseInit 和 文件一样,(和 )文件将参与基于文件系统的自动路由。

¥Just like redirect, it accepts a status code or a ResponseInit as the second parameter:

redirectDocument(path, 301);
redirectDocument(path, 303);
redirectDocument(path, {
  headers: {
    "Set-Cookie": await commitSession(session),
  },
});
Remix v2.17 中文网 - 粤ICP备13048890号