Bug in staticfiles' HashedFileMixin?

38 views
Skip to first unread message

Bernhard Mäder

unread,
Dec 6, 2014, 3:29:53 PM12/6/14
to django...@googlegroups.com
Hey guys,

HashedFileMixin's pattern variable looks like it could be extended for other file types:

class HashedFilesMixin(object):    
    default_template
= """url("%s")"""
    patterns
= (
       
("*.css", (
            r
"""(url\(['"]{0,1}\s*(.*?)["']{0,1}\))""",
           
(r"""(@import\s*["']\s*(.*?)["'])""", """@import url("%s")"""),
        )),
    )


But - if I read the code right - it can't. Because in post_process, the patterns are applied to all files matched by any of the extensions. 

What I like to be able to achieve is get cache busting in some of my js files as well. I marked all static file references in my code with a 'staticFile()' call and then tried to customize django's CachedStaticFilesStorage like this:

class MyCachedStaticFilesStorage(django.contrib.staticfiles.storage.CachedStaticFilesStorage):
    patterns
= django.contrib.staticfiles.storage.HashedFilesMixin.patterns + (
       
(u'*.js', [
           
(r"""(staticFile\(['"]{0,1}\s*(.*?)["']{0,1}\))""", r"""staticFile("%s")""", )
       
]),
   
)


As of today, that won't work, since all CSS rules are applied to my JS, too, which causes some stuff to be replaced that shouldn't.

Is this a bug, and should I file a bug report? Is it worth fixing? How else can I get cache busting into JS files?

Thanks,
Bernhard
 

Collin Anderson

unread,
Dec 9, 2014, 8:24:09 AM12/9/14
to django...@googlegroups.com
Hi Bernhard,

Yes, on quick glance this looks like a bug. It looks like all replacements get applied to all matching files. Feel free to open a ticket if you haven't. (If you have, feel free to link to it in case someone else finds this.)

Collin
Reply all
Reply to author
Forward
0 new messages