编程语言
首页 > 编程语言> > javascript – 使路由器与动态路由在浏览器直接链接上给出404

javascript – 使路由器与动态路由在浏览器直接链接上给出404

作者:互联网

我有一个React Front-end连接到Wordpress API,路由在客户端使用React Router定义.当我使用Link将它们引导到动态路线时,它们可以正常工作.但是,当我使用浏览器地址栏直接访问按Enter键的链接,或者我刷新.i得到

404 page not found

我知道这是因为直接调用服务器正在发生,并且当我们有PHP服务器时,没有javascript来操作共享主机上的react-router操作.

考虑反应快照,但它不适合API的动态性质.请问任何人都可以给出解决这个问题的方法吗?

解决方法:

看看这个:https://github.com/rafrex/spa-github-pages

这个概念可能对你有帮助.

When the GitHub Pages server gets a request for a path defined with frontend routes, e.g. example.tld/foo, it returns a custom 404.html page. The custom 404.html page contains a script that takes the current url and converts the path and query string into just a query string, and then redirects the browser to the new url with only a query string and hash fragment.

标签:create-react-app,react-router-v4,javascript,reactjs
来源: https://codeday.me/bug/20191002/1841427.html