django static files versioning

后端 未结 10 1089
伪装坚强ぢ
伪装坚强ぢ 2021-01-30 21:26

I\'m working on some universal solution for problem with static files and updates in it.

Example: let\'s say there was site with /static/styles.css file - a

10条回答
  •  猫巷女王i
    2021-01-30 21:48

    The major advantage of this solution: you dont have to modify anything in the templates.

    This will add the build version into the STATIC_URL, and then the webserver will remove it with a Rewrite rule.

    settings.py

    # build version, it's increased with each build
    VERSION_STAMP = __versionstr__.replace(".", "")
    # rewrite static url to contain the number
    STATIC_URL = '%sversion%s/' % (STATIC_URL, VERSION_STAMP)
    

    So the final url would be for example this:

    /static/version010/style.css
    

    And then Nginx has a rule to rewrite it back to /static/style.css

    location /static {
        alias /var/www/website/static/;
        rewrite ^(.*)/version([\.0-9]+)/(.*)$ $1/$3;
    }
    

提交回复
热议问题