Ant-design-pro: 关于本地browserHistory问题,刷新报错

Created on 10 Jan 2018  ·  23Comments  ·  Source: ant-design/ant-design-pro

index.js 中使用browserHistory,刷新后就报错了

import browserHistory from 'history/createBrowserHistory';
const app = dva({
  history: browserHistory(),
  onError,
});

当在首页或其他链接刷新报 Uncaught SyntaxError: Unexpected token < 错误

我在npm start 下运行的, 还想问一个问题, 项目中有index.html ?
app.start('#root'); app 组件应该挂载在 id = root 的元素中,但我没找到index.html ,

🛑 bug

Most helpful comment

All 23 comments

有 index.ejs
使用createBrowserHistory 后,遇到同样问题,但并不是每次个链接刷新都出错,只有 http://localhost:8000/user/login 登录成功之后跳转会出错。

browserHistory 需要服务端支持。

https://pro.ant.design/docs/deploy-cn#%E5%89%8D%E7%AB%AF%E8%B7%AF%E7%94%B1%E4%B8%8E%E6%9C%8D%E5%8A%A1%E7%AB%AF%E7%9A%84%E7%BB%93%E5%90%88

好的,我测试下。

@afc163 之前不论是hashHistory还是browserHistory,本地开发都没问题的,不需要服务器配置,roadhog升级以后就出现这个问题了,只能用hashHistory,是不是index.html的问题,现在生成的index.html中'index.js'都是相对路径,而不是'/index.js',系统找不到 /user/index.js

@afc163 经测试,确实是这样, 请测试

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Ant Design Pro</title>
  <link rel="icon" href="/favicon.png" type="image/x-icon">
</head>

<body>
  <div id="root"></div>
<script type="text/javascript" src="index.js"></script></body>

</html>

@sorrycc

<script type="text/javascript" src="index.js"></script></body>

这串是 html-webpack-plugin 注入的吧?是否能改成 /index.js 得看下他的配置,https://github.com/jantimon/html-webpack-plugin#configuration

原来是 /index.js,这里应该算是一个升级不兼容的问题。

找到原因了,配个 publicPath: '/' 就好了,html-webpack-plugin 会加上 publicPath 前缀的。

roadhog@1 dev 模式下的 publicPath 始终是 /,所以没这个问题。

@afc163 默认配个publicPath 吧。

@chenshuai2144 @sorrycc 请问publicPath在哪里加,是在html-webpack-plugin中加吗,我加了如下

"html-webpack-plugin": {
      "version": "2.30.1",
      "publicPath": "/",
      ...
      ...
      },

刷新还是出错,求解

在 .webpackrc里面配置一下

在 .webpackrc 里配

@sorrycc 云谦大大。。
.webpackrc 里面hash:true 有什么作用

感谢@chenshuai2144 @sorrycc 两位大哥
不过我当时是用的❯browser history simple (Simple version of Ant Design Pro with browser history)选项安装的ant-design-pro,即如下

? which type of scaffold do you want to new ?
  standard (All functional Ant Design Pro scaffold)
  simple (Simple version of Ant Design Pro with hash history)
❯ browser history simple (Simple version of Ant Design Pro with browser history)

这种方式安装的没有.webpackrc,standard (All functional Ant Design Pro scaffold)是有.webpackrc的,我需要安装什么包吗

这是个脚手架??

经测试,修改.webpackrc文件后,刷新没问题,但我关闭mock后,使用云服务器如下:

export default (noProxy
  ? { "/*": "http://mywebsite/" }
  : delay(proxy, 1000));

npm run start:no-proxy
进入任意路由如:http://localhost:8000/form/basic-form,后刷新,出现:

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Thu Jan 11 11:21:51 CST 2018
There was an unexpected error (type=Not Found, status=404).
No message available

求解

一个很简单的404呀。 你的路径配错了呀。
你怎么可以/*这样配置。。。
静态资源还是应该请求给本地的。
你可以给spring 设置一个basePath。然后/base : /mysit/base.

您好,antd-pro2.0好像没有搜索到webpackrc.js文件,请问应该在哪个文件里配置打包的路径呢?

@lwt0023 2.0 框架改为 umi,可以参考 umi 文档在 config/config.js 中配置。

Was this page helpful?
0 / 5 - 0 ratings

Related issues

renyi818 picture renyi818  ·  3Comments

yaoleiroyal picture yaoleiroyal  ·  3Comments

wuyongdec picture wuyongdec  ·  3Comments

zhuanglong picture zhuanglong  ·  3Comments

lvzheng0404 picture lvzheng0404  ·  3Comments