【问题标题】:React Router browserHistory works on local, not on productionReact Router browserHistory 在本地工作,而不是在生产环境
【发布时间】:2016-08-12 15:22:56
【问题描述】:

每当我在本地使用 browserHistory 时,我都没有问题,但是当我在发货前对其进行测试时,我会得到一个带有错误的空白页面。因此,当我用 hashHistory 替换 browserHistory 时,一切正常,但我失去了漂亮的 url。

未捕获的安全错误:无法在“历史”上执行“replaceState”:无法在原始文档中创建 URL 为“file:///Users/somefile/work/someproject/index.html”的历史状态对象空'和网址。

const Routes = (
  <Router history={browserHistory}>
    <Route path="/" component={Login} />
    <Route path="/content" component={App} >
      <Route path="/component1" component={component1} />
      <Route path="/component2" component={component2} />
      <Route path="/component3" component={component3} />
    </Route>
  </Router>
);

在我的 index.js 中

const createStoreWithMiddleware = applyMiddleware(ReduxPromise)(createStore);
const store = createStoreWithMiddleware(reducers);

ReactDOM.render(
  <Provider store={store}>
  {Routes}
  </Provider>,
  document.querySelector('.react-internal')
)

在我的 app.js 中

export default class App extends Component {

  render() {
    return (
      <div>
      <Header />
      <div className="container-fluid">
        {this.props.children}
        </div>
      </div>
    );
  }
}

还有我的 webpack 文件

var webpack = require('webpack');

module.exports = {
  devtool:'source-map',
  entry: [
    './src/index.js',
  ],
  output: {
    path: __dirname,
    publicPath: '/',
    filename: 'bundle.js'
  },
  module: {
    loaders: [
      {
        exclude: /node_modules/,
        loaders: ['react-hot','babel']
      },
      { test: /\.css$/, loader: "style!css" },
      {
        test: /\.(jpe?g|png|gif|svg)$/i,
        loaders: [
            'file?hash=sha512&digest=hex&name=[hash].[ext]',
            'image-webpack?bypassOnDebug&optimizationLevel=7&interlaced=false'
        ]
      }
    ]
  },
  resolve: {
    extensions: ['', '.js', '.jsx']
  },

  devServer: {
    port:8080,
    historyApiFallback: true,
    contentBase: './'
  }
};

【问题讨论】:

  • 本地测试时,是通过网络服务器运行还是直接点击 HTML 文件?
  • 当我在本地运行它时,我在 webpack 开发服务器上运行它。在我运行 webpack 并捆绑它之后,我通过单击 index.html 文件来测试它

标签: javascript reactjs react-router


【解决方案1】:

我必须添加一个基本 url 并链接到该文件夹​​。我认为这完全取决于您如何部署

import { createHistory } from 'history'


const history = useRouterHistory(createHistory)({
  basename: '/projectFolder'
})
const Routes = (
  <Router history={history} >

    <Route path="/" component={Login} />
    <Route path="/content" component={App} >
      <Route path="/component1" component={component1} />
      <Route path="/component2" component={component2} />
      <Route path="/component3" component={component3} />
    </Route>
  </Router>
);

【讨论】:

  • 嗨,/projectFolder 路径应该是什么?
【解决方案2】:

您还需要考虑您在哪里部署项目。例如,如果您部署到 firebase,则必须在 firebase.json 文件中添加此“目标”:“/index.html”代码。包含这行代码后,网址将正常工作

firebase.json

{
 "hosting": {       
   "public": "public/dist", 
   "rewrites": [ {
     "source": "**",   
     "destination": "/index.html"  // this line of code is very important, which makes your url work in production. This code means that no matter what url is, direct all processing in index.html
   } ]
 }

}

【讨论】:

    猜你喜欢
    • 2016-04-28
    • 2016-07-16
    • 2018-03-23
    • 1970-01-01
    • 2019-04-18
    • 2019-08-17
    • 1970-01-01
    • 2021-12-08
    • 2014-04-27
    相关资源
    最近更新 更多