I have a React app that I want to reside on a specific url route prefix of a Flask app.
Examples:
host.com/is the home page of the Flask app. It'sindex.htmlfile in thetemplatesdirectory (using standard Flask conventions here).host.com/aboutis another page of the Flask app. It's some other file in the sametemplatesdirectory.host.com/searchis the root of the React app. I want anything with/searchprefix to reference some part of the React app.host.com/search/123is some page of the React app (following the above logic). I am usingreact-router-domfor routing inside React app.
I built the React app with npm run build and then tried serving just the React app with Flask like in this example. Everything worked fine.
Then I attempted the following to put React at specific url prefix of the Flask app:
# file app.py
import os
from flask import Flask, render_template
from .search_blueprint import search_blueprint
app = Flask(__name__)
app.register_blueprint(search_blueprint, url_prefix='/search')
@app.route('/')
def index():
return render_template('index.html')
if __name__ == '__main__':
app.run(use_reloader=True, port=5000, threaded=True)
And here are the contents of the search_blueprint.py file (very similar to the answer to the question that I already mentioned:
# file search_blueprint.py
from flask import Blueprint, send_from_directory
import os
build_dir = os.path.abspath('./app/static/build')
search_blueprint = Blueprint('search', __name__, static_folder=build_dir)
# Serve React App
@search_blueprint.route('/', defaults={'path': ''})
@search_blueprint.route('/<path:path>')
def serve(path):
if(path == ""):
return send_from_directory(build_dir, 'index.html')
else:
if(os.path.exists(os.path.join(build_dir, path))):
return send_from_directory(build_dir, path)
else:
return send_from_directory(build_dir, 'index.html')
The Flask index.html page that is referenced in app.py loads fine when I go to host.com/. When I go to host.com/search I get a blank screen (meaning no errors from Flask). In the console there's an error message like this:
Loading failed for the <script> with source “http://host.com/static/js/main.ca656ef1.js”.
Anyone knows why I can't get this React app work at a specified url prefix?
Here's the directory layout relevant to the question:
app
├───static
│ └───build
├───templates
│ └───index.html
├───app.py
└───search_blueprint.py
Edit
Adding contents of build directory:
build
│ asset-manifest.json
│ favicon.ico
│ index.html
│ manifest.json
│ service-worker.js
│
└───static
├───css
│ main.d293ea0a.css
│ main.d293ea0a.css.map
│
├───js
│ main.ca656ef1.js
│ main.ca656ef1.js.map
│
└───media
logo.5d5d9eef.svg
<script>source should behttp://host.com/static/build/main.ca656ef1.jsbut it'shttp://host.com/static/js/main.ca656ef1.js. What is in the build folder? Are you using webpack? How are the scripts being added to your index.html page?create-react-appand fully resides withinstaticdirectory. Not sure how exactly scripts are added but when following answer to this question to serve just the React app with Flask, everything works. I am simply moving that logic to a Blueprint to try to move the app under a specified url prefix. I added contents ofbuilddirectory.