长路径在使用react-router-dom时无法加载

huangapple go评论70阅读模式
英文:

Long path fails to load with react-router-dom

问题

我有一个VerifyEmail路由,它以JWT令牌作为参数。然而,它无法加载。有没有办法让它加载?如果令牌很短,大约10个字符长,路由会加载。但是如果令牌更长,就无法工作。我想知道react-router-dom对URL路径的长度是否有限制,是否会导致这个问题。

在我的App.jsx中:

import React from "react";
import { BrowserRouter, Routes, Route } from "react-router-dom";
import Home from "./components/Home";
import Login from "./components/Login";
import Logout from "./components/Logout";
import SignUp from "./components/SignUp";
import Navbar from "./components/Navbar";
import VerifyEmail from "./features/users/VerifyEmail";
import { selectCurrentToken } from './features/auth/authSlice';
import { useSelector } from "react-redux/es/hooks/useSelector";

function App() {
  const auth = useSelector(selectCurrentToken);

  return (
    <>
      <BrowserRouter>
        <Navbar auth={auth} />
        <Routes>
          <Route index element={<Home />} />
          {!auth && <Route path="/login" element={<Login />} />}
          {auth && <Route path="/logout" element={<Logout />} />}
          <Route path="/signup" element={<SignUp />} />
          <Route path="/verify/:token" element={<VerifyEmail />} />
        </Routes>
      </BrowserRouter>
    </>
  );
}

export default App;

在我的VerifyEmail.jsx中:

import React from 'react';
import { useParams } from 'react-router-dom';

const VerifyEmail = () => {
  const { token } = useParams();

  return (
    <div>{token}</div>
  );
}

export default VerifyEmail;

当我尝试访问这个链接时,我得到一个错误的请求。

http://localhost:5173/verify/eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6ImUiLCJpYXQiOjE2OTE1NDkwMjcsImV4cCI6MTY5MTU3MDYyN30.HqPtksRbGTxxP8EeFs7XLHfHQLBXleaVil6MYmiKB3Y

(https://i.stack.imgur.com/zhuzb.png)

英文:

I have a VerifyEmail route that takes on a JWT token as a parameter. However, it does not load. Is there anyway to get it to load? The route loads if the token is short, about 10 characters long. Anything longer it does not work. I was wondering if there's a limit to the length of the url path for react-router-dom to be causing this problem.

In my App.jsx

import React from &quot;react&quot;;
import { BrowserRouter, Routes, Route } from &quot;react-router-dom&quot;;
import Home from &quot;./components/Home&quot;
import Login from &quot;./components/Login&quot;
import Logout from &quot;./components/Logout&quot;
import SignUp from &quot;./components/SignUp&quot;
import Navbar from &quot;./components/Navbar&quot;
import VerifyEmail from &quot;./features/users/VerifyEmail&quot;
import { selectCurrentToken } from &#39;./features/auth/authSlice&#39;
import { useSelector } from &quot;react-redux/es/hooks/useSelector&quot;;

function App() {
  const auth = useSelector(selectCurrentToken)

  return (
    &lt;&gt;
      &lt;BrowserRouter&gt;
        &lt;Navbar auth={auth} /&gt;
        &lt;Routes&gt;
          &lt;Route index element={&lt;Home /&gt;} /&gt;
          {!auth &amp;&amp; &lt;Route path=&quot;/login&quot; element={&lt;Login /&gt;} /&gt;}
          {auth &amp;&amp; &lt;Route path=&quot;/logout&quot; element={&lt;Logout /&gt;} /&gt;}
          &lt;Route path=&quot;/signup&quot; element={&lt;SignUp /&gt;} /&gt;
          &lt;Route path=&quot;/verify/:token&quot; element={&lt;VerifyEmail /&gt;} /&gt;
        &lt;/Routes&gt;
      &lt;/BrowserRouter&gt;
    &lt;/&gt;
  )
}

export default App

In my VerifyEmail.jsx

import React from &#39;react&#39;
import { useParams } from &#39;react-router-dom&#39;

const VerifyEmail = () =&gt; {
  const { token } = useParams()

  return (
    &lt;div&gt;{token}&lt;/div&gt;
  )
}

export default VerifyEmail

When I try this link, I get a bad request.

http://localhost:5173/verify/eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6ImUiLCJpYXQiOjE2OTE1NDkwMjcsImV4cCI6MTY5MTU3MDYyN30.HqPtksRbGTxxP8EeFs7XLHfHQLBXleaVil6MYmiKB3Y

(https://i.stack.imgur.com/zhuzb.png)

答案1

得分: 0

网络服务器错误地将您的jwt与文件混淆了。它如何区分locahost:5173/favicon.icolocalhost:5173/U3MDYyN30.HqPtksRbGTxxP8EeFs7之间的区别。因此,它会抛出一个Not Found的错误,就像您在图片中显示的那样。您有两个选项来处理这个问题:

  1. 如果您正在使用webpack,您可以配置禁用点规则(文档)。
 historyApiFallback: {
    disableDotRule: true
 }
  1. 您可以修改路由并将JWT作为查询参数传递。
<Route path="/verify" element={<VerifyEmail />} />

在您的VerifyEmail组件中,使用useQuery来获取token的值。

// http://localhost:5173/verify?token=abcxyx
import { useQuery } from 'react-router-dom';

function VerifyEmail() {
   let query = useQuery();
   let token = query.get('token');

   // 其余组件逻辑
}
英文:

The web server is misinterpret your jwt with a file. How is it know the difference between locahost:5173/favicon.ico and localhost:5173/U3MDYyN30.HqPtksRbGTxxP8EeFs7. So it will throw an error Not Found as you show in the image. You have two options to handle this:

  1. If you are using webpack, you can config to disable the dot rule (docs).
 historyApiFallback: {
    disableDotRule: true
 }
  1. You can modify the route and pass the JWT as a query parameter
&lt;Route path=&quot;/verify&quot; element={&lt;VerifyEmail /&gt;} /&gt;

And in your VerifyEmail component, use useQuery to get the value of token

// http://localhost:5173/verify?token=abcxyx
import { useQuery } from &#39;react-router-dom&#39;;

function VerifyEmail() {
   let query = useQuery();
   let token = query.get(&#39;token&#39;);

   // Rest of your component logic
}

huangapple
  • 本文由 发表于 2023年8月9日 11:25:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76864365.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定