Can I create routes with react-router for a github-pages site?

前端 未结 5 859
灰色年华
灰色年华 2020-12-03 20:01

Ok, so I\'ve made a SPA using React and React-Router and have pushed it to github pages, but none of the routes I have written work when I refresh or click back in my browse

5条回答
  •  醉酒成梦
    2020-12-03 21:03

    I just found a solution for this without using the HashRouter. I created a little node script that's called before my deployment script that creates a 404.html file with the same content as the index.html. Github pages serves that file if you refresh on a page in your react-app.

    const fs = require('fs')
    fs.copyFile('build/index.html', 'build/404.html', (err) => {
      if (err) throw err
      console.log('index.html was copied to 404.html')
    })
    

提交回复
热议问题