Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PipelineCachedStorage generates hashed files, but generated URLs do not have hashes #638

Open
john-clarke opened this issue Aug 10, 2017 · 1 comment

Comments

@john-clarke
Copy link

PipelineCachedStorage generates hashed files during collectstatic, but the generated URLs reference the files without hashes.

Example:

Post-processed 'js/app-build.js' as 'js/app-build.efa204cafb67.js'

But the rendered django template references app-build.js instead of app-build.efa204cafb67.js

STATICFILES_FINDERS = [
    'pipeline.finders.FileSystemFinder',
    'pipeline.finders.AppDirectoriesFinder',
    'pipeline.finders.PipelineFinder',
    'pipeline.finders.CachedFileFinder',
]

STATICFILES_STORAGE = 'pipeline.storage.PipelineCachedStorage' 

CACHES = {
    ...
   'staticfiles': {
      'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
      'LOCATION': 'staticfiles',
      'TIMEOUT': 3600 * 24 * 8,
      'MAX_ENTRIES': 1000,
    }
}

django = 1.10.5
django-pipeline = 1.6.12

@tonkolviktor
Copy link

since this is a real pain, as a dirty fix, I've wrote the following code in settings.py

if sys.argv[1:2] != ["collectstatic"]:
    css_out = STATIC_ROOT / PIPELINE['STYLESHEETS']['css_files']['output_filename']
    js_out = STATIC_ROOT / PIPELINE['JAVASCRIPT']['js_files']['output_filename']
    css_replace_done, js_replace_done = False, False
    if css_out.exists():
        for f in css_out.parent.iterdir():
            if f.name != css_out.name and css_out.stem in f.name:
                print(f'Found css versioned file: {f.name}')
                css_replace_done = True
                PIPELINE['STYLESHEETS']['css_files']['output_filename'] = PIPELINE['STYLESHEETS']['css_files']['output_filename'].replace(css_out.name, f.name)
    if js_out.exists():
        for f in js_out.parent.iterdir():
            if f.name != js_out.name and js_out.stem in f.name:
                print(f'Found js versioned file: {f.name}')
                js_replace_done = True
                PIPELINE['JAVASCRIPT']['js_files']['output_filename'] = PIPELINE['JAVASCRIPT']['js_files']['output_filename'].replace(js_out.name, f.name)
    if not css_replace_done or not js_replace_done:
        raise ValueError('css and js versioned files were not found, make sure collectstatic was called')

It tries to find the hashed files and replaces the output config, which then will be added to the template.
This code assumes one has only one css and js config with the names hard-coded.

It solved the problem for me now, but of course a proper fix from the library would be the ideal way

anyhow happy coding

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants