当多个 Remix.run 加载器都抛出重定向时会发生什么?

kst*_*tis 1 reactjs remix.run

浏览 Remix.run 的常见问题解答,我注意到保护路线的推荐方法是强制加载程序throw使用redirect.

但我发现很难理解的是,当多个加载器为单个路由抛出重定向时会发生什么?在这种情况下,预期的浏览器行为是什么?

示例:假设访问/protected-route涉及调用 2 个加载程序。两个加载程序都配置为在用户未经身份验证时throw重定向。/login在这种情况下浏览器会发生什么?一条路线预计会访问两次吗?如果重定向位置不同怎么办?

Kil*_*man 5

请记住,Remix 会处理重定向。即使它并行调用所有加载器,它仍然会等到它们全部解析后再进行处理。如果任何加载器响应是重定向,它将重定向到路由树中从根到叶路由的第一个重定向。因此/parent加载器重定向将取代/parent/child加载器重定向。

// call loaders in parallel from root to leaf route
const responses = await Promise.all([loaderRoot, loaderParent, loaderChild])

// find first response that is a redirect
const redirectResponse = responses.find(response => isRedirect(response))
if (redirectReponse) {
  return redirectResponse
}
// no redirects so continue processing
Run Code Online (Sandbox Code Playgroud)