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

Catch-all route doesn't work when static_folder ends in forwardslash #3452

Closed
Sti2nd opened this issue Dec 19, 2019 · 2 comments · Fixed by #3456
Closed

Catch-all route doesn't work when static_folder ends in forwardslash #3452

Sti2nd opened this issue Dec 19, 2019 · 2 comments · Fixed by #3456
Milestone

Comments

@Sti2nd
Copy link

@Sti2nd Sti2nd commented Dec 19, 2019

I am trying to serve a React app with Flask. The React app uses React Router which means some routes should simply be handled by React. To achieve this one can usually use a catch-all route that returns the index.html. I first couldn't get the catch-all route to work at all, and then I got it working by removing the trailing slash for the static_folder parameter when calling Flask.

Expected Behavior

When one enter a path with trailing forward slash in the static_folder parameter for flask.Flask() it shouldn't affect whether a catch-all route goes into effect or not.

In the below code remove the trailing slash for the static_folder and you are able to reach the catch-all route (home function) by entering a random route like /something.

from flask import Flask, render_template

app = Flask(__name__, static_folder="frontend/build/", template_folder="frontend/build")

@app.route("/")
def route1():
    return "route1"


@app.route("/<path:path>")
def home(path):
    return "routeAny"

Actual Behavior

Steps to reproduce

  1. Run the above flask app
  2. Open browser and verify that it renders the text "route1" for route /
  3. Browse to /something and see that you get 404 Not Found.

Environment

  • Python version: 3.7.
  • Flask version: 1.1.1
  • Werkzeug version: 0.16.0
@Sti2nd
Copy link
Author

@Sti2nd Sti2nd commented Dec 20, 2019

I updated my code above after realising a logical flaw in my code. This comment is about the logical flaw in my code.
I found out the reason that the static files didn't work without a trailing slash was because I serve index.html when requesting each of these static files. So I changed my code to serve the static files if they exist. (dir_path = Path().absolute())

@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def catch_all(path):
    build_folder = dir_path.joinpath("frontend", "build")
    if path != "" and os.path.exists(dir_path.joinpath(build_folder, path)):
        if path.count("/") > 1:
            [path, filename] = path.rsplit("/", maxsplit=1)
            return send_from_directory(dir_path.joinpath(build_folder, path), filename)
        else:
            filename = path
            return send_from_directory(dir_path.joinpath(build_folder), filename)
    else:
        return render_template("index.html")

The bug still remains that a catch-all route won't catch anything when there is a trailing slash in the static_folder parameter.

@davidism
Copy link
Member

@davidism davidism commented Apr 3, 2020

Finally released 1.1.2 with this.

nicolaiarocci added a commit to pyeve/eve that referenced this issue Apr 19, 2020
Flask 1.1.2 changed its behavior around static endpoints. Now, an
eventual forward slash ('/') at the end of the url will be stripped out.

See pallets/flask#3452
nicolaiarocci added a commit to pyeve/eve that referenced this issue Apr 19, 2020
Flask 1.1.2 changed its behavior around static endpoints. Now, an
eventual forward slash ('/') at the end of the url will be stripped out.

See pallets/flask#3452
nicolaiarocci added a commit to pyeve/eve that referenced this issue Apr 19, 2020
Flask 1.1.2 changed its behavior around static endpoints. Now, an
eventual forward slash ('/') at the end of the url will be stripped out.

See pallets/flask#3452
jonahkagan added a commit to votingworks/arlo that referenced this issue Apr 23, 2020
There was a bugfix for the static folder logic that caused a test
failure. See more details here: pallets/flask#3452
jonahkagan added a commit to votingworks/arlo that referenced this issue Apr 23, 2020
There was a bugfix for the static folder logic that caused a test
failure. See more details here: pallets/flask#3452
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 14, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants