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

README suggestion: how to use in Heroku #16

Closed tgs closed 3 years ago

tgs commented 3 years ago

Hello! I had a hard time googling how to do static asset compilation in Heroku, but eventually found that there's a hook program that gets run after all of the packages are installed. I created bin/post_compile with these contents:

#!/usr/bin/env bash
set -e

cd "$1"  # the build dir
echo "-----> Compiling static assets"
flask digest compile

And that runs the compilation on Heroku. It's helpful because Heroku deploys with a git push, so without it you'd have to add the compiled files to git, which would be annoying. Not sure if you'll feel it's worth cluttering your README with this, but thought I would suggest it.

Also, thanks for this package! Very easy to integrate, works very well so far. Awesome!

References: https://github.com/heroku/heroku-buildpack-python/blob/main/bin/steps/hooks/post_compile https://vitorbaptista.com/how-to-compile-sass-scss-files-when-deploying-django-to-heroku

nickjj commented 3 years ago

Hi,

Thanks.

I could add a FAQ item for this. But with Heroku there's also the heroku.yml file at https://devcenter.heroku.com/articles/build-docker-images-heroku-yml. But I don't know how Heroku deals with entrypoint scripts. It might need a release script to copy the assets into a different directory from inside the Docker image (since the digesting happens during the Docker build process).

Do you have any experience with building Docker images on Heroku?

tgs commented 3 years ago

Sorry, I don't know anything about Heroku and Docker, only Python deployment

nickjj commented 3 years ago

Before adding this to the README, do you happen to know the difference between pre and post compile? These scripts aren't mentioned in the docs.

In both cases will this compilation step happen before traffic gets served by that dyno?

tgs commented 3 years ago

Looks like the pre_compile script runs before the Python dependencies have been installed -

remote: Compressing source files... done.
remote: Building source:
remote: 
remote: -----> Building on the Heroku-20 stack
remote: -----> Python app detected
remote: -----> Running pre-compile hook
remote: This is the time when the pre-compile script executes
remote: -----> No change in requirements detected, installing from cache
remote: -----> Installing pip 20.1.1, setuptools 47.1.1 and wheel 0.34.2
remote: -----> Installing SQLite3
remote: -----> Installing requirements with pip

(I put echo "This is the time when the pre-compile script executes" as the pre_compile script)

In both cases, this is during the process of compiling the "slug," before any dyno gets started with the new code.

nickjj commented 3 years ago

Thanks. Based on that the post compile hook looks like the way to go.

I've added the FAQ item to the readme at: https://github.com/nickjj/flask-static-digest#how-do-you-use-this-extension-with-heroku

tgs commented 3 years ago

Looks great, thank you!!

nickjj commented 3 years ago

No problem. I actually just pushed a minor update which echos what's happening by adding echo "-----> Digesting static files" before running the command since based on your output it seems like a best practice to describe each command.

Let me know if it works and looks good the next time you deploy please.

tgs commented 3 years ago

I used the exact "wording" from the README and it works great:

remote: -----> Running post-compile hook
remote: -----> Digesting static files
remote: Check your digested files at '/tmp/build_6e532ef0/static'

:+1: :+1: