react-router的browserHistory/react-router-dom的BrowserRouter刷新页面404问题解决

前端解决:

  • '/' 表示把所有的url都发给代理https://api.example.com
  • bypass 表示不需要发给发给代理服务器的条件

  如下配置,可以监听https://api.example.com域下的/开头的请求(等效于所有请求),然后判断请求头中accept字段是否包含html,若包含,则代理请求至/index.html,随后将返回index.html文档至浏览器。

 

proxy: {
  '/': {
    target: 'https://api.example.com',
    secure: false,
    bypass: function(req, res, proxyOptions) {
      if (req.headers.accept.indexOf('html') !== -1) {
        console.log('Skipping proxy for browser request.');
        return '/index.html';
      }
    }
  }
}

 

django后端解决

对于所有后端不存在的url直接返回index.html文档给浏览器

handler404 = view.page_not_found


# 强制刷新前端功能
def page_not_found(request, exception, template_name='index.html'):
    return render(request, template_name)

 

 

参考:

http://blog.codingplayboy.com/2017/12/26/react-router-browserhistory-404/

转载于:https://www.cnblogs.com/shengulong/p/10321478.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值