2

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's index.html file in the templates directory (using standard Flask conventions here).

  • host.com/about is another page of the Flask app. It's some other file in the same templates directory.

  • host.com/search is the root of the React app. I want anything with /search prefix to reference some part of the React app.

  • host.com/search/123 is some page of the React app (following the above logic). I am using react-router-dom for 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
2
  • From your directory layout, your js <script> source should be http://host.com/static/build/main.ca656ef1.js but it's http://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? Commented Mar 22, 2018 at 21:29
  • Not using webpack. React app was created using create-react-app and fully resides within static directory. 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 of build directory. Commented Mar 22, 2018 at 21:37

1 Answer 1

1
app = Flask(__name__, static_folder='app/static/build')

You have to specify that static directory when you create the Flask app. This was driving me insane so I hope it helps you and others.

Cheers

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.