score:9

Accepted answer

so! it turns out heroku support team was incorrect in their analysis of my application. my application is built in two different ways (one for production as well as for development). using npm run start [see static/package.json] on local utilizes hot reloading and benefits from faster local changes via server.js. however, in a production environment, you want to use a compressed bundle.js file so my goal was to use npm run build:production [see static/package.json].

the issue i was running into was syntaxerror: expected expression, got '< bundle.js:1 in the console and it seemed to me that bundle.js wasn't loading at all. i listed a series of valid questions above on why i thought that might happen, but they all assumed that the main problem was an inability to run my react application at the same time as my flask application.

i was totally wrong. i didn't need to run server.js at all. the real reason that index.html and flask/python wasn't able to find my bundle.js and load the frontend on production was because of a mistake in the config.py file within flask which i never thought to post.

flask has a very particular configuration that allows static_folder to be defined and template_folder. a while back i had swapped my static_folder for another directory while working on some image upload functionality. the reason i never caught it is because on local i run server.js for hot reloading so i never saw the compressed bundle.js file error out.

after fixing this mistake, i pushed to heroku and amazingly...it worked on the first try!

here's the correct code that fixed it:

app = flask(__name__, static_folder="./static/dist", template_folder="./static")

in closing whilst running a flask/react application on heroku:

  • use multi buildpacks (one for node, one for python).
  • use your procfile to load only the flask side.
  • you need a package.json file in your root directory...even if you have another in your static folder like me.
  • use webpack to compress your react code and serve it up in a compressed way.
  • use render_template with flask to render the index.html file that holds you root div for react.
  • make sure all your dependencies for react are listed inside of actual "dependencies" instead of "devdepencies" otherwise heroku will ignore them.

i really hope this helps someone! i was slamming my head against the wall for 2 weeks and it turned out to be a small obscure problem....isn't it always?

additional resources: https://codeburst.io/creating-a-full-stack-web-application-with-python-npm-webpack-and-react-8925800503d9 while this is very simplified...it's what lead me to finding my bug so i'll post it here.

score:1

your flask backend doesn't seem to be running on the same port as you forward your traffic too:

starting gunicorn 19.6.0 listening at: 0.0.0.0:13521 (4) 

app.all(/^\/api\/(.*)/, (req, res) => {
    proxy.web(req, res, { target: 'http://0.0.0.0:8081' });
});

flask is listening on port 13521 (chosen randomly) but the /api/ routes are forwarded to port 8081. instead, you should probably configure gunicorn to use a fixed port: http://docs.gunicorn.org/en/latest/settings.html#bind


Related Query

More Query from same tag