react-hot-loader: react-hot-loader-minimal-boilerplate does not work with Flask server
I am working on an app with a Flask backend and a React frontend, and I was unable to get hot reloading working. I then tried modifying the react-hot-loader-minimal-boilerplate example to work with a Flask backend, and then hot reloading stopped working as well, and I had to Ctrl-F5 it in order to get the changes to show. The Flask server code is given below:
from flask import Flask, render_template
app = Flask(__name__, static_url_path='',
template_folder='', static_folder='dist')
@app.route('/')
def main():
return render_template('index.html')
if __name__ == '__main__':
app.run(debug=True, reloader_type='stat', port=8080)
I also modified package.json, and moved the script to start:
{
"scripts": {"start": "webpack-dev-server --hot --inline --progress"},
"name": "react-hot-loader-minimal-boilerplate",
"version": "1.0.0",
"description": "",
"main": "index.js",
"repository": {
"type": "git",
"url": "git+https://github.com/wkwiatek/react-hot-loader-minimal-boilerplate.git"
},
"keywords": [],
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/wkwiatek/react-hot-loader-minimal-boilerplate/issues"
},
"homepage": "https://github.com/wkwiatek/react-hot-loader-minimal-boilerplate#readme",
"dependencies": {
"react": "^15.4.2",
"react-dom": "^15.4.2",
"react-router-dom": "^4.2.2"
},
"devDependencies": {
"babel-core": "^6.23.1",
"babel-loader": "^6.3.2",
"babel-polyfill": "^6.23.0",
"babel-preset-latest": "^6.22.0",
"babel-preset-react": "^6.23.0",
"react-hot-loader": "^3.0.0-beta.6",
"webpack": "^2.2.1",
"webpack-dev-server": "^2.4.1"
}
}
Any help is appreciated, thanks!
EDIT: I should additionally note that I built the app.js file using the “webpack -w” command.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 24
There is no dependence on Flask or any backend in this library. I think you may have this a bit confused. In development, you have the
webpack-dev-serverwhich serves two filesindex.htmlandmain.js. With just the basic set-up, it assumes NO backend API. It looks like here you are trying to use Flask to also serveindex.htmlandmain.js(although I see no code right now that additionally servesmain.js) This will not work. You cannot use bothwebpack-dev-serverand Flask. You have to usewebpack-dev-server. The only use for Flask is to proxy API requests and it looks like now you are not even usingwebpack-dev-serverbecause you are runningwebpack -w.I suggest doing this instead:
webpack-dev-serverlike in the example. Copy exactly the example.HOST = 0.0.0.0/apitolocalhost:5000Here is an example config for the dev server with proxy mode(I am using Flask for an API server in project). It has
socket.ioalso, you may not need that.@alexanderwhatley There is no hot reloading in production. All of this is just for development. You don’t need to hot reload anything. In production, you use webpack to bundle
main.jsand then all you’re doing is hosting the bundledindex.htmlandmain.json a web server. A popular set-up for instance using Flask is usingnginxas a reverse proxy, hostingmain.jsandindex.html, and then ‘reverse proxying’ any API requests to a gunicorn server.