nickjj / flask-static-digest

Flask extension to help make your static files production ready by md5 tagging and gzipping them.
MIT License
157 stars 27 forks source link

Working with Blueprints #20

Closed ericsouza closed 3 years ago

ericsouza commented 3 years ago

Hey, thanks for this extensions. I wonder how I would work with many blueprints. I tried some things but was no able to make it work

Basically I have an app like that:

└── flaskr ├── app.py ├── my_blueprint │   ├── blueprint.py (this is where my blueprint its configured and with the routes passing the view_func) │   └── views.py (with the handler functions that call render_template) ├── static │   └── my_blueprint │   ├── css │   │   └── stylesheet.css │   ├── images │   │   └── image.png │   └── js │   └── main.js └── templates └── my_blueprint └── index.html

With this organization I can have many blueprint and each blueprint with its own templates and static folder (similar to what Django does). Do you have any example like that? thanks a lot!!

nickjj commented 3 years ago

Hi,

You can have as many blueprints and template directories as you'd like but yeah this extension is only going to look in a single static directory to digest files. Mainly because in most applications you'd output your bundles to 1 specific directory (your static directory) and Flask will serve them from there. How you bundle those files are up to you.

ericsouza commented 3 years ago

I was able to compile, but tried different settings to blueprint, some cases the static_url_for method didn't match the filename to the file on the cache_manifest, other cases I just got a 404 loading the asset. I just wonder how I should config my blueprint.

Basically I was not able to set my blueprint this way:

my_blueprint = Blueprint(
    "my_blueprint", 
    __name__, 
    static_folder="/static/my_blueprint",
    static_url_path="/static/my_blueprint", 
    template_folder="../templates/my_blueprint"
    )

and use static_url_for like that:

<link rel="stylesheet" href="{{static_url_for('my_blueprint.static', filename='css/style.css')}}">

the static_url_for does not match filename to cache_manifest and fallback to the file without md5 hash. If I change the above code to this:

<link rel="stylesheet" href="{{static_url_for('static', filename='my_blueprint/css/style.css')}}">

It works just fine. But I don't think its the best options. Do you know if I am doing some mistake?

nickjj commented 3 years ago

The static_url_for helper directly calls url_for btw. It would be interesting if it behaved differently than url_for. With your first example if you use url_for instead (temporarily of course), do you get the same 404?

ericsouza commented 3 years ago

with url_for I get the normal file, without md5 hash. If I understood correctly this extensions uses the filename to make the match with cache_manifest.json and find the md5 version. My cache_manifest.json look like this:

{"my_blueprint/css/style.css": "my_blueprint/css/style-1688c8210b6509d702b1adb96bc4d0f3.css"}

This explains why this work:

<link rel="stylesheet" href="{{static_url_for('static', filename='my_blueprint/css/style.css')}}">

But this doesn't:

<link rel="stylesheet" href="{{static_url_for('my_blueprint.static', filename='css/style.css')}}">

I'm pretty sure that the problem is the way I think static_folder and static_url_for arguments for Blueprint is wrong. So I will just stick without using my_blueprint.static when using static_url_for. Thanks for your time!! :)

nickjj commented 3 years ago

Yeah this extension will add the md5 hash but I was curious if {{ url_for('my_blueprint.static', filename='css/style.css' )} worked in your case, in the sense that it didn't 404.

Flask's docs mention using my_blueprint.static with url_for will work as long as your Blueprint has a url_prefix set.

ericsouza commented 3 years ago

I had no 404 with {{ url_for('my_blueprint.static', filename='css/style.css' )}

Well, thanks for the help, I will close the issue

nickjj commented 3 years ago

No problem.

Sounds like I should add a FAQ item in the docs on how to reference static files within a Blueprint.

GhostChasser commented 3 years ago

hi there, any updates on this one ? @ericsouza how did you compile files from blueptrints ?

ericsouza commented 3 years ago

hi there, any updates on this one ? @ericsouza how did you compile files from blueptrints ?

I endup splitting the frontend from the backend, so my flask application didn't have to deal with static files