Javascript 在React中实现硬重载

Javascript 在React中实现硬重载,javascript,php,reactjs,symfony,routing,Javascript,Php,Reactjs,Symfony,Routing,我有一个在apache/PHP服务器上运行的React应用程序 React应用程序在主路径上加载。也就是说,PHP被配置为在加载React应用程序的/路径上加载index.html index.html // .... <div id="react-hook"></div> <script> const wrapper = document.getElementById("react-hook"); wrapper ? ReactDOM.re

我有一个在apache/PHP服务器上运行的React应用程序

React应用程序在主路径上加载。也就是说,PHP被配置为在加载React应用程序的
/
路径上加载
index.html

index.html

// ....
<div id="react-hook"></div>

<script>
    const wrapper = document.getElementById("react-hook");
    wrapper ? ReactDOM.render(<BrowserRouter><App /></BrowserRouter>, wrapper) : false;
</script>
并使用一些链接呈现它们

<Link to="/>Home</Link>     
<Link to="/login>Login</Link>
<Link to="/test>Test</Link>

为react应用程序实现硬重新加载的正确方法是什么

这取决于您使用的服务器。您不想将所有请求转发到
/
。无论收到什么请求,您都希望显示
/index.html
。如果用户请求
/test
,服务器会给他们
/index.html
,但会调用
/test
。他们没有404,也没有301。他们得到了
/test
的状态码200,但得到了
/index.html
的内容

对于nginx,这类似于
try\u files$uri index.html

对于Apache,您可以使用
mod_rewrite
重写所有不存在的路径(类似于
RewriteCond%{REQUEST_FILENAME}!-f
的条件是文件不存在以便进行重写)
/index.html
RewriteRule.*/index.html


React应用程序只需要相同的入口点(
),页面加载上的React路由器将使用页面URL进行相应导航,因此所有路由都使用相同的
index.html
到达目的地。

好的,我是gochya!是的,我有点不正确的想法。我正在使用Apache,因此我可以在
.htaccess
中执行此操作-一个问题是,我实际上不想为所有请求提供
index.html
,因为我想将该服务器用作react的API-因此一些路由将是API端点。但是我可以将它们写入重定向规则(
/api/*
通过php获得服务)
<Link to="/>Home</Link>     
<Link to="/login>Login</Link>
<Link to="/test>Test</Link>
behavior: /test (hard route) ---> / (soft route)

should be: /test (hard route) ---> /test (soft route)