Opened 9 years ago

Last modified 9 years ago

#26249 closed Bug

ManifestStaticFilesStorage crashes on absolute URLs — at Version 1

Reported by: Aymeric Augustin Owned by: nobody
Component: contrib.staticfiles Version: 1.9
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Aymeric Augustin)

To reproduce, enable ManifestStaticFilesStorage:

STATIC_ROOT= '...'
STATIC_URL = '/static/'
STATICFILES_DIRS = ['...']
STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.ManifestStaticFilesStorage'

Create a.css in one of STATICFILES_DIRS with this content:

@font-face{font-family:A;src:url(/static/a.woff) format("woff");}

Create a a.woff file next to a.css.

Then collectstatic crashes with this stack trace:

Post-processing 'a.css' failed!

Traceback (most recent call last):
  File "/Users/myk/.virtualenvs/project/bin/django-admin", line 11, in <module>
    sys.exit(execute_from_command_line())
  File "/Users/myk/.virtualenvs/project/lib/python3.5/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "/Users/myk/.virtualenvs/project/lib/python3.5/site-packages/django/core/management/__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/myk/.virtualenvs/project/lib/python3.5/site-packages/django/core/management/base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/myk/.virtualenvs/project/lib/python3.5/site-packages/django/core/management/base.py", line 399, in execute
    output = self.handle(*args, **options)
  File "/Users/myk/.virtualenvs/project/lib/python3.5/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 176, in handle
    collected = self.collect()
  File "/Users/myk/.virtualenvs/project/lib/python3.5/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 128, in collect
    raise processed
  File "/Users/myk/.virtualenvs/project/lib/python3.5/site-packages/django/contrib/staticfiles/storage.py", line 245, in post_process
    content = pattern.sub(converter, content)
  File "/Users/myk/.virtualenvs/project/lib/python3.5/site-packages/django/contrib/staticfiles/storage.py", line 184, in converter
    hashed_url = self.url(unquote(joined_result), force=True)
  File "/Users/myk/.virtualenvs/project/lib/python3.5/site-packages/django/contrib/staticfiles/storage.py", line 131, in url
    hashed_name = self.stored_name(clean_name)
  File "/Users/myk/.virtualenvs/project/lib/python3.5/site-packages/django/contrib/staticfiles/storage.py", line 280, in stored_name
    cache_name = self.clean_name(self.hashed_name(name))
  File "/Users/myk/.virtualenvs/project/lib/python3.5/site-packages/django/contrib/staticfiles/storage.py", line 94, in hashed_name
    (clean_name, self))
ValueError: The file 'a.css/a.woff' could not be found with <django.contrib.staticfiles.storage.ManifestStaticFilesStorage object at 0x105a34240>.

Using url(./a.woff) instead of url(/static/a.woff) avoids the issue.

It looks like HashedFilesMixin.url_converter doesn't handle that case gracefully, so we end up with a.css/a.woff instead of just a.woff. When a URL starts with STATIC_URL, perhaps it should strip it?

(I ended up with the /static/ prefix because I set webpack's output.publicPath to /static/. I don't remember why I need it, but it doesn't make the bug less valid.)

(EDIT: I need to set output.publicPath to /static/ because webpack's code splitting doesn't work without this -- chunks get loaded from /current/url/chunk.js instead of /static/chuck.js, which obviously doesn't work. I don't want to include the hostname in the URL because I want to use the same build in staging and production.).

Change History (1)

comment:1 by Aymeric Augustin, 9 years ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top