Search code examples
pythondjangodjango-compressor

Django-Compressor Compressed Filename


Working with Django-compressor. Wondering if there's anyway to get the compressed filename so that I can load the script asynchronously (as django-compressor currently doesn't support that).

E.g. So I can do something like this in my template

var compressed = {{ COMPRESSED_JS_FILENAME|undefined }};
if (compressed) {
    var script = document.createElement('script');
    script.src = compressed;
    script.setAttribute('async', True);
    document.documentElement.firstChild.appendChild(script);
}

Python Version: 2.7
Django Version: 1.4.1


Solution

  • Alternative Solution:

    Couldn't find a way to do this, so instead I added hook for the page to go over the tags and basically look for the ones that had been compressed and add an async tag to them. Psuedocode/Idea

    content = BeautifulSoup(page)
    scripts = [tag.extract() for tag in soup.find_all('script')]
    for script in scripts:
        old_script = script
        src = script.get('src')
        if '/COMPRESSED/' in src:
            script['async'] = "True"
        page = page.replace(old_script, script)