Code Monkey home page Code Monkey logo

Comments (14)

jezdez avatar jezdez commented on July 30, 2024

Hmm, I'm not sure I can follow, are you saying you want the URL that compressor uses in the link to the compressed files to be dynamic?

If yes, you probably can easily override the COMPRESS_STORAGE setting to a custom subclass of the default storage backend (compressor.storage.CompressorFileStorage) whose url() method would need to return a different URL depending on some kind of other value, e.g. a project specific setting, say MYPROJECT_COMPRESS_VERSION.

from django-compressor.

jasondavies avatar jasondavies commented on July 30, 2024

I tried the custom subclass approach, but I also need to be able to detect HTTPS so I know whether to return https://www. (I only have one SSL certificate) or use the http://media{0,1,2,...} URLs. I couldn't see a way to access the current request object from the custom subclass.

from django-compressor.

jezdez avatar jezdez commented on July 30, 2024

Okay, I suggest the following changes to make this happen:

Add the two settings with the approporiate default values:

COMPRESS_CSS_BACKEND = 'django_compressor.css.CssCompressor'
COMPRESS_JS_BACKEND = 'django_compressor.js.JsCompressor'

Then in the template tag these settings would be used to figure out, which backend to call. The template context is then passed to the backend's __init__ and set as the self.extra_context dictionary. Later, you're able to use that in an overwritten output or output_inline method in an own subclass of the backend.

For deciding whether to generate HTTP or HTTPS URLs that context would also be beneficial. I wonder if that should be tried to be automatically converted depending on request.is_secure(). What do you think?

from django-compressor.

jezdez avatar jezdez commented on July 30, 2024

#93 was a dupe of this.

from django-compressor.

dboris avatar dboris commented on July 30, 2024

Is it possible to use different urls (eg subdomains) based on whether the page was served over HTTP or HTTPS if the compressor was run in offline mode?

from django-compressor.

jezdez avatar jezdez commented on July 30, 2024

@dboris, yeah, set COMPRESS_URL to '//yourdomain.com/'. The '//' at the beginning will automatically be picked up by the browser -- assuming both https://yourdomain.com and http://yourdomain.com point to the same folder.

from django-compressor.

dboris avatar dboris commented on July 30, 2024

When I set COMPRESS_URL to be different from STATIC_URL, I get an error when compressing offline:

Error: An error occured during rendering: '/media/css/impromptu.css' isn't accesible via COMPRESS_URL ('//media.mysite.com/media/') and can't be compressed

This file is included in the template like this:
<link href="{{ STATIC_URL }}css/impromptu.css" rel="stylesheet" type="text/css">

Am I still missing something?

from django-compressor.

smusiol avatar smusiol commented on July 30, 2024

Is it possible to compress css and send to external server with images and also to compress js files but leave them on app server?

from django-compressor.

poswald avatar poswald commented on July 30, 2024

I also get the "isn't accesible via COMPRESS_URL " error when compressing offline while setting the COMPRESS_URL to be different from STATIC_URL. This error doesn't make sense to me because I don't see why the command should care if a file is accessible via a url when compressing in offline mode. It makes it sound like it's trying to load it via HTTP. Is this just a bad description of the issue?

I expected/want my workflow to be:

  • I specify static resources like this: <link rel="stylesheet" href="{{STATIC_URL}}css/base.css" type="text/css">
  • I run ./manage.py collectstatic to get everything collected into the static dir of the local filesystem, ready to be served out.
  • I run ./manage.py compress to create the files in the static/CACHE/* directory
  • I sync the entire static directory to the CDN using django-cumulus ./manage.py syncstatic

I expect:

  • The STATIC_URL setting stays /static/ which is the default
  • The COMPRESS_URL setting is http://mycdn.com/static/ and is what is output into the html for the browser.
  • files on the local filesystem under /var/mypath/static/* as specified by STATIC_ROOT
  • files in the CDN filesystem under mycontainer/static/*
  • references to the compressed files in the html as src="http://mycdn/static/CACHE/*"
  • references to some of the non-compressable files in the html as src="http://mycdn/static/*"

I cannot find a way to make the settings such that it allows this workflow. I guess the problem is how the cache key is computed? Furthermore, is this all going to change when Django 1.4 introduces the {% static %} tag?

from django-compressor.

rbdcti avatar rbdcti commented on July 30, 2024

It seems that at this point Django-compressor is not able to be used as-is in cases where:

  1. remote storage to a CDN is used
  2. SSL is required
  3. The CDN uses different hostnames for SSL and non-SSL access (e.g. Cloudfiles, Amazon S3, and others) (e.g. https://c43982.ssl.rackcdn.com) is different than the non-ssl site base (e.g. http://c43982.r43.rackcdn.com)

This would be a pretty common usecase for anyone with a production site that stores site media on a CDN. Between this ticket and #93 I see a number of potential solutions. In my case, I'm using offline mode, as well as a CDN (CloudFiles). The different hostnames makes the "//myhost.com" solution not workable, as well as the issue with the "isn't accesible via COMPRESS_URL" check in base.py, since the hostnames are different and I can only set COMPRESS_URL to one of them.

So is there a solution to this that doesn't require hacking up the code? I see 1.2.0dev introduced a new 'compressed' context dictionary to the templates, any goodies there? Or is jezdez's method from Feb 15th the way to go...which seems to require forking and hacking up the django-compressor code...I'd have to remove that check in base.py as well methinks.

Optimally I'd love to see a way that doesn't require modifying django-compressor. If it does, let's find a way so that we can push the changes required back into django-compressor if at all possible.

from django-compressor.

rbdcti avatar rbdcti commented on July 30, 2024

Guys, I figured this out for my case above (offline files, all static media on a CDN with two separate domains for SSL and non-SSL). It's a bit hacky, but it works for me. What I basically do is have a script that:

  • Runs manage.py compress once with the HTTP/non-SSL settings. This will generate the files in CACHE/ and automatically upload them to the CDN (keeping them locally as well). A manifest.json is created as well.
  • Reads manifest.json and takes it into a python dict with simplejson
  • Runs manage.py compress AGAIN, this time passing in an env var that I detect in my settings and use to switch STATIC_URL and COMPRESS_URL to be the SSL domain (with django-cumulus I also have to set CUMULUS_USESSL = True as well)
  • From the result of the last run of manage.py compress, we'll have any resources that are different due to the host path change generated and uploaded to the CDN also, as well as a manifest.json file with the https:// paths and different hashes. The script will read this into a python dict as well, and combine the two dicts to form the "master" manifest dict.
  • This master manifest dict is written out to manifest.json, replacing the one that is there
  • manifest.json is copied to the CDN and an edge purge is initiated

This seems to work fine! All of the SSL and non-SSL resources are written to the CDN, and the manifest.json contains entries for both. I combined this with the redis-based manifest.json caching I am doing, and the result is awesome.

from django-compressor.

BrianHicks avatar BrianHicks commented on July 30, 2024

Was this solved in django-compressor? I seem to be having the same problem using S3.

from django-compressor.

yeago avatar yeago commented on July 30, 2024

The below almost works. I'm noticing that the manifest.ssl.json doesn't contain the https urls for some reason. thoughts welcome.

Okay, so I came up with a solution a bit different than for OFFLINE_COMPRESS + alternate http/https @rbdrbd

Firstly, get hip with django's 1.4 addition if you're behind nginx (like you probably are)
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTOCOL', 'https')

Then add this middleware

class StaticFix(object):
    def process_request(self, request):
        if request.is_secure():
            settings.COMPRESS_OFFLINE_MANIFEST = 'manifest.ssl.json'
    def process_response(self, request, response):
        if request.is_secure():
            response.content = response.content.replace(settings.STATIC_URL,
                settings.STATIC_URL.replace("http", "https"))
        return response

Then run, in addition to normal compress django manage.py compress --settings=ssl_settings

#ssl_settings.py
from settings import *
COMPRESS_OFFLINE_MANIFEST = 'manifest.ssl.json'
STATIC_URL = COMPRESS_URL = STATIC_URL.replace('http', 'https')

from django-compressor.

nkeilar avatar nkeilar commented on July 30, 2024

@jezdez was this solved, the issue has been closed but I can't glean the preferred way to solve this issue.

from django-compressor.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.