Limit App Engine to Minified JS and CSS

Call me paranoid or call me efficient, but I don’t like non-minified CSS and Javascript accessible in a production environment. It’s one thing setting up your web application to use minified files when it’s rolled out , but I’d prefer that the originals aren’t available at all – and yes, part of that is me not wanting to make it too easy for people to snoop through my code (though I do release a lot as open source anyway).

In any event, this proved a little tricky when using Google App Engine for LocationFu. Google’s deploy process has the option of excluding certain application files from being sent up, but it can’t exclude any files that sit within a directory marked as a static path. The solution is simple, but took me a while to figure out. Previously, I had my static path set up like so in my app.yaml file (note that this is for Python App Engine):

- url: /static
static_dir: static

That will map any file starting with /static into the static directory in the app tree. Instead, you can map individual files, and limit the extensions you want to serve:

- url: /static/(.*?)
static_files: static/\1
upload: static/(.*\.(gif|png|jpg|min\.css|min\.js))

That tells App Engine to grab the file name requested and map it into the static directory, while the upload parameter includes a regex to match only selected file extensions (.min.js, but not just .js, for example). This won’t stop your app from serving any file while running on the local development server, but when pushed to production, your JS and CSS files will only be accessible in minified form.

  • Bryce Darling

    Perhaps you know how to solve this problem. I attempted to use a slightly modified version of your above solution, but only for my images folder and not all static resources.

    The config:

    - url: /images/(.*?)
    static_files: static/images/1
    upload: static/images/(.*.(gif|png|jpg))$

    The resulting error:

    Fatal error when loading application configuration:
    Unable to assign value 'static/images/(.*.(gif|png|jpg))$' to attribute 'upload':
    Value 'static/images/(.*.(gif|png|jpg))$' does not match expression '^(?!^).*(?!$).$'

    Thanks, I really like your idea!

    • http://bradkellett.com Brad Kellett

      Apologies, you can't use the $ operator in your regex. I've corrected the original post, it should work fine if you drop that off the end.

  • http://www.stayinathens.com PanosJee

    but how do you generate them ?
    do you do it locally manually ?