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

Favicon fetching incompatible with django-storages S3 #128

Closed
samjeckert opened this issue Jul 10, 2021 · 5 comments
Closed

Favicon fetching incompatible with django-storages S3 #128

samjeckert opened this issue Jul 10, 2021 · 5 comments
Assignees
Labels
bug Something isn't working

Comments

@samjeckert
Copy link

The generated favicon URL is broken when the static resource is stored on S3 via django-storages. The code here adds ?v={version} to the end of the URL, but django-storages already has to add parameters to authenticate with S3. The result is a final URL with two ?'s, which does not work.

https://<s3_bucket>.s3.amazonaws.com/admin-interface/favicon/favicon.ico?AWSAccessKeyId=<access_key_id>&Signature=&Expires=?v=0.15.6

@fabiocaccamo fabiocaccamo self-assigned this Jul 10, 2021
@fabiocaccamo fabiocaccamo added the bug Something isn't working label Jul 10, 2021
@merwok
Copy link
Contributor

merwok commented Jul 11, 2021

Just a precision: this project is compatible with django-storages and S3, if you are using cloudfront (a public distribution) to host your images.

@c0dezli
Copy link

c0dezli commented Jul 22, 2021

Same problem here

@bastiaan85
Copy link

One option to resolve this may be to implement a custom vstatic tag to support a version parameter, like shown here. Eg quick and dirty:

# django < 2.1
from django.contrib.staticfiles.templatetags.staticfiles import static
# django >= 2.1
from django.templatetags.static import static

@register.simple_tag
def vstatic(path, version):
    url = static(path)
    sep = '&' if '?' in url else '?'
    return f'{url}{sep}v={version}'

or with proper url parsing

from urllib.parse import urlencode, urlparse, parse_qsl, urlunparse

# django < 2.1
from django.contrib.staticfiles.templatetags.staticfiles import static
# django >= 2.1
from django.templatetags.static import static

@register.simple_tag
def vstatic(path, version):
    url = static(path)
    parsed = urlparse(url)
    values = dict(parse_qsl(parsed.query))
    if 'v' in values or 'version' in values:    # if you mean to make the provided version optional
        return url
    parsed = parsed._replace(query=urlencode(values))
    return urlunparse(parsed)

then in favicon.html

<script type="text/javascript" src="{% vstatic 'admin_interface/favico/favico-0.3.10-patched.min.js' version %}"></script>

@fabiocaccamo
Copy link
Owner

fabiocaccamo commented Sep 4, 2021

What about just removing the version parameter?
Then to avoid caching issues it would be enough to use the built-in django ManifestStaticFilesStorage.

@fabiocaccamo
Copy link
Owner

Fixed within 0.17.1 version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants