Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask, serving a React application: cannot refresh "pages"

So I am serving a index.html file from Flask. The index.html file comes from a built project using: https://github.com/facebookincubator/create-react-app

I have a couple routes setup in the React app, for example: "/users" and "/contracts"

When I refresh one of these routes, I get a 404 from flask, but while "clicking" on links found on the site, they work perfectly fine.

like image 689
Sebastian Karlsson Avatar asked Oct 27 '25 07:10

Sebastian Karlsson


1 Answers

When you are clicking the links in the interface, React is re-rendering the page without any server-side intervention, and then updating the route in the URL bar. When loading however, you are making that route request to the server direct, and Flask does not have that route registered.

Simplest way is to register these routes in the decorator for the function serving your homepage view

@app.route('/')
@app.route('/users')
@app.route('/contracts')
def home_page():

If there are many many routes, you may want to use the catch-all URL pattern.

like image 181
Chris Applegate Avatar answered Oct 28 '25 21:10

Chris Applegate



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!