Next.js and Flask are two popular web frameworks. While each of them can work on their own there are good reasons why you would like to combine them. I recently did on gym.realfeedback.app the best place to get feedback on gym exercises by professional coaches.
The obvious reason to use Next.js is for serverside rendering of a React application in order to get all the benefits like instant loading time and better SEO capabilities compared to a standard React application.
The reason to use Flask is if you already have experience using if you simply like using Python on the backend side, like data processing.
If you use Flask you most likely run it in uwsgi with Nginx. Here we will configure nginx in way to forward any frontend calls to a Node.js instance running Next.js wheres the backend is forwarded to Flask.
For this we need to adapt the server section of our nginx.conf file a follows:
https://gist.github.com/sonium0/123d3085c8d5e870d2f3e05bb5c91e36
With this the frontend calls are forwarded to an instance of Next.js running port 3000.
Calls to /api however, like form submission or uplaods are handled by Flask.
With this we have a working configuration that combines Next.js with Flask.
If this guide has been helpful to me, don’t forget to leave a comment or a clap.