问题描述
我在使用带有webpackdevserver的react路由器来设置单页react应用程序时遇到一些问题.如果使用浏览器历史记录,则webpack在输入嵌套的路由网址(例如/client/view)时会出现一些问题.可以通过添加apiFallback来解决,但是热重载仍然存在问题.它尝试从不存在的深层网址(/client/view/hot-update.json)加载hot-update.json文件,因此失败并重新加载页面.我如何告诉hotreload始终从基本URL(/)加载hot-update.json?
I have some problems setting up a single page react app using react router with the webpackdevserver. If I use browserhistory webpack has some problems when entering a nested route url (/client/view for example). This could be solved adding apiFallback but hot reload does still have a problem. It tries to load the hot-update.json file from the deep url (/client/view/hot-update.json) which does not exist and therefor fails and reloads the page. How can i tell hotreload to always load the hot-update.json from the base url (/)?
推荐答案
在使用''
以外的publicPath
时,我遇到了类似的问题,我通过在devServer
选项中添加代理条目来解决此问题:
I had a similar issue when using a publicPath
other than ''
, which I solved by adding a proxy entry to the devServer
options:
devServer: {
// ... rest of options
proxy: {
'/myPublicPath/*': {
target: 'http://localhost:8080/',
pathRewrite: { '^/myPublicPath': '' },
}
}
除此之外,请确保同时设置output.publicPath
和devServer.publicPath
并使其相等.
Other than that, make sure both output.publicPath
and devServer.publicPath
is set and equal.
希望这会有所帮助!
这篇关于启用单页应用程序反应热重装webpack的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!