React-router v4 - cannot GET *url*

前端 未结 8 643
梦如初夏
梦如初夏 2020-12-07 14:04

I started to use react-router v4. I have a simple in my app.js with some navigation links (see code below). If I navigate to localhost/voca

相关标签:
8条回答
  • 2020-12-07 14:38

    I was having the same issue, what fixed it for me was editing my package.json file, and under scripts: {

    "build": "webpack -d && copy src\\index.html dist\\index.html /y && webpack-dev-server --content-base src --inline --port 3000"
    

    at the end of my webpack build code I added --history-api-fallback this also seemed like the easiest solution to the Cannot GET /url error

    Note: the next time you build after adding --history-api-fallback you will notice a line in the output that looks like this (with whatever your index file is):

    404s will fallback to /index.html

    0 讨论(0)
  • 2020-12-07 14:40

    If your application is hosted on a static file server, you need to use a instead of a .

    import { HashRouter } from 'react-router-dom'
    
    ReactDOM.render((
      <HashRouter>
        <App />
      </HashRouter>
    ), holder)
    

    https://github.com/ReactTraining/react-router/blob/v4.1.1/FAQ.md#why-doesnt-my-application-render-after-refreshing

    0 讨论(0)
  • 2020-12-07 14:45

    It worked for me just need just affffding devServer { historyApiFallback: true } is OK, not need use publicPath: '/'

    usage like:

      devServer: {
        historyApiFallback: true
      },
    
    0 讨论(0)
  • 2020-12-07 14:46

    I'm assuming you're using Webpack. If so, adding a few things to your webpack config should solve the issue. Specifically, output.publicPath = '/' and devServer.historyApiFallback = true. Here's an example webpack config below which uses both of ^ and fixes the refresh issue for me. If you're curious "why", this will help.

    var path = require('path');
    var HtmlWebpackPlugin = require('html-webpack-plugin');
    
    module.exports = {
      entry: './app/index.js',
      output: {
        path: path.resolve(__dirname, 'dist'),
        filename: 'index_bundle.js',
        publicPath: '/'
      },
      module: {
        rules: [
          { test: /\.(js)$/, use: 'babel-loader' },
          { test: /\.css$/, use: [ 'style-loader', 'css-loader' ]}
        ]
      },
      devServer: {
        historyApiFallback: true,
      },
      plugins: [
        new HtmlWebpackPlugin({
          template: 'app/index.html'
        })
      ]
    };
    

    I wrote more about this here - Fixing the "cannot GET /URL" error on refresh with React Router (or how client side routers work)

    0 讨论(0)
  • 2020-12-07 14:47

    I also had success with this by adding ... historyApiFallback: true

    devServer: {
        contentBase: path.join(__dirname, "public"),
        watchContentBase: true,
        publicPath: "/dist/",
        historyApiFallback: true
    }
    
    0 讨论(0)
  • 2020-12-07 14:49

    Just to supplement Tyler's answer for anyone still struggling with this:

    Adding the devServer.historyApiFallback: true to my webpack config (without setting publicPath) fixed the 404/Cannot-GET errors I was seeing on refresh/back/forward, but only for a single level of nested route. In other words, "/" and "/topics" started working fine but anything beyond that (e.g. "/topics/whatever") still threw a 404 on refresh/etc.

    Just came across the accepted answer here: Unexpected token < error in react router component and it provided the last missing piece for me. Adding the leading / to the bundle script src in my index.html has solved the issue completely.

    0 讨论(0)
提交回复
热议问题