Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flask, Build, and accessing the Static directories #3976

Closed
cinjon opened this issue Feb 5, 2018 · 6 comments
Closed

Flask, Build, and accessing the Static directories #3976

cinjon opened this issue Feb 5, 2018 · 6 comments

Comments

@cinjon
Copy link

cinjon commented Feb 5, 2018

Hi there. I'm having an awful time trying to figure out what I've messed up.

The result of running npm run build is that I have a production directory at foo/app/build/. This includes all of the good stuff, e.g. static/, index.html, manifest.json, etc. My flask app is served from foo/__init__.py and declared with app = Flask(__name__, static_folder="./app/build/static", template_folder="./app/build").

As usual, the index.html in app/build is the entry point, and it loads React at /static/js/main.<hash>.js. This works as expected. So does the /static/css/* files and so forth. What I mean by that is that if I try to serve localhost from the build, then I can visit http://localhost:5000/static/js/main..js and it will load the minified js.

However, nothing else in foo/app/build can be seen and loaded, even though the server is returning 200s when I request the root:

@a.app.route('/', methods=['GET'])
def index():
    return render_template('index.html')

@a.app.route('/<path:path>', methods=['GET'])
def any_root_path(path):
    return render_template('index.html')

This includes the inex.html, service-worker.js, the favicon.ico, and the manifest.json. If I run npm start and use a proxy to talk to the client at port 3000 and the server at port 5000 (on localhost), then everything works. That of course won't fly for production.

Any idea what's going on?

@gaearon
Copy link
Contributor

gaearon commented Feb 5, 2018

This seems like a question about Flask more than about CRA. So I suggest asking it on Flask forums. We don't really know Flask API and can't advise about that.

That said I don't see the code for serving actual static files. Your handlers appear to always serve index.html no matter what's requested. Maybe that's the problem?

@gaearon gaearon closed this as completed Feb 5, 2018
@cinjon
Copy link
Author

cinjon commented Feb 5, 2018

Regarding your question re where the problem might be, while your suggestion might be an issue, that's actually why I'm asking here. It finds the index.html and it finds the static js/css files. Given that, I would think it should be loading the React app from the client as well. I'm instead just seeing the index.html with the noscript tag and nothing on the page.

It doesn't seem like a PUBLIC_URL issue as it's talking to the server and getting the files.

@gaearon
Copy link
Contributor

gaearon commented Feb 5, 2018

Sorry, it's hard to say more without seeing an example. Your explanation isn't 100% clear in regards to what it is exactly that you see.

@marcrleonard
Copy link

@cinjon any luck with this? I'm battling with the exact same problem.

@cinjon
Copy link
Author

cinjon commented Feb 19, 2018

It ended up being an unrelated issue somewhere else in the code.

@CharlyJazz
Copy link

@cinjon My flask example with CRA build
https://github.com/CharlyJazz/TalkCode/blob/master/app/controllers/web/web.py

@lock lock bot locked and limited conversation to collaborators Jan 20, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants