HTTP标头react-router

Eri*_*Ven 5 http-headers react-router

到目前为止,我还没有找到我的问题的答案,所以希望大家知道答案。我正在寻找一种在React中设置HTTP标头的简单方法。我认为将它们直接设置在路由器内部将是一个不错的解决方案:

render((
  <Router history={browserHistory}>
    <Route path="/" component={App}>
      <Route path="about" component={About}/>
      <Route path="users" component={Users}>
        <Route path="/user/:userId" component={User}/>
      </Route>
      <Route path="*" component={NoMatch}/>
    </Route>
  </Router>
), document.getElementById('root'))
Run Code Online (Sandbox Code Playgroud)

可能作为属性或作为额外的标记<Header>,因此您可以轻松地为每个路由指定要设置的标头。例如,对于所有路线(path="*"):

内容安全政策:script-src'self'https: //apis.google.com

只是一个主意。是否有支持这种功能的模块?或如果不是这样,是否有任何原因呢?

小智 0

这不能直接在 React 内部完成;它必须在托管应用程序的服务器上完成。可以为 React 应用程序内的调用(例如 API 调用)设置标头,但不能为外部调用设置标头。React 运行应用程序,但不托管它们。