React Router v7 已發布。 檢視文件
redirectDocument

redirectDocument

這是 redirect 的一個小型封裝器,它將觸發文件層級的重新導向到新的位置,而不是客戶端導覽。

當您在同一個網域上,有一個 Remix 應用程式與一個非 Remix 應用程式並存,並且需要從 Remix 應用程式重新導向到非 Remix 應用程式時,這非常有用。

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 });
};

就像 redirect 一樣,它接受一個狀態碼或一個 ResponseInit 作為第二個參數。

redirectDocument(path, 301);
redirectDocument(path, 303);
redirectDocument(path, {
  headers: {
    "Set-Cookie": await commitSession(session),
  },
});
文件和範例以 MIT