django-cookie-consent icon indicating copy to clipboard operation
django-cookie-consent copied to clipboard

Unable To Minify / Compress Changing JavaScript Into 1 File

Open 9mido opened this issue 4 years ago • 8 comments

This package has JavaScript code that creates a new cookie age for how long a cookie will be in the user's browser when they accept. When cookies are not accepted, and the user keeps refreshing the page with a cookie banner shown, new files get inserted into the CACHE folder that django-compressor uses (not sure if django-pipeline or similar packages does the same thing). This seems to be because each cookie javascript code has a brand new timestamp for the cookie age that generates each time the page refreshes. Because of the new timestamps on each page refresh, the cookie JavaScript code changes each time, thus creating a new CACHE file instead of just having one. This could be problematic if the website has tons of users visiting the site since it will create lots of cache files (minified javascript files) instead of just reading from one JavaScript file where the code does not change.

This isn't really a big deal if users decide not to minify / compress their website. But maybe there is a way to deal with this? If not, let me know and feel free to close the issue I just won't try to compress the JavaScript this package uses.

9mido avatar May 19 '20 21:05 9mido

@9mido can you post sample code for this behavior? I can not remember any issue with django-compressor.

bmihelac avatar May 21 '20 07:05 bmihelac

	{% load compress %}	
	{% compress js %}     
	<script nonce="{{request.csp_nonce}}">
          var cookie_groups = [];
          {% for cookie_group in cookie_groups %}
            cookie_groups.push("{{ cookie_group.varname }}");
          {% endfor %}

          $(document).ready(function() {
            $.showCookieBar({
              content: "{% filter escapejs %}{% with cookie_groups=cookie_groups|join:", " %}<div class="cookie-bar">This site uses cookies. Do you agree to use cookies? <br/> <a href="{{ url_accept }}" class="cc-cookie-accept">Accept</a> <a href="{{ url_decline }}" class="cc-cookie-decline">Decline</a> <a href="{{ url_cookies }}">Cookies</a></div>{% endwith %}{% endfilter %}",
              cookie_groups: cookie_groups,
              cookie_decline: "{% get_decline_cookie_groups_cookie_string request cookie_groups %}",
              beforeDeclined: function() {
                document.cookie = "{% get_decline_cookie_groups_cookie_string request cookie_groups %}";
              }
            });
          });
        </script>

	{% endcompress %}
COMPRESS_ENABLED = True
COMPRESS_CSS_FILTERS = ['compressor.filters.css_default.CssAbsoluteFilter', 'compressor.filters.cssmin.rCSSMinFilter']
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    # other finders..
    'compressor.finders.CompressorFinder',
)

When refreshing the browser when cookie bar is shown, project_name/static_root/CACHE/js/ directory will keep populating minified files.

9mido avatar May 21 '20 18:05 9mido

@9mido is compressing that code needed at all? These are just two statements, and it looks to me that it is better if they are inline, then in separate resource.

bmihelac avatar May 22 '20 08:05 bmihelac

@bmihelac Not sure that is why I wanted to ask here to see your thoughts. I don't mind if it is not compressed and I probably won't do it if it becomes difficult to do. Wondering if this code can be re-written to be able to be compressed as one file? If not, no worries.

9mido avatar May 22 '20 17:05 9mido

@9mido not sure, but if you find a solution PR would be welcomed :)

bmihelac avatar May 25 '20 06:05 bmihelac

@bmihelac

I did not test this, but I see that cloudflare has an 'Auto Minify' configuration option to minify css, html, and javascript. Also not sure how nginx or apache compression would affect it. If someone enables these options, I would imagine that a similar problem might occur or maybe even an error of some kind.

9mido avatar Aug 26 '20 19:08 9mido

A sane approach would be to emit divs or other elements with data-* attributes, and have the JS run based on that. I'll attach this to the 1.0 milestone, as I'm afraid it's too big of a refactor for 0.4.0

There's a chance we bump it to 0.5.0 if that would be the first next version.

Other than that, it would also make sense to have the JS for this library properly published/distributed as ES-modules so that it can be integrated in toolchains like webpack, while including a ready-to-go bundle for ease of use.

sergei-maertens avatar Sep 04 '22 21:09 sergei-maertens

Hi @9mido - I've just released django-cookie-consent 0.5.0b0, and I believe that release provides what you need to resolve your minification issues, as it essentially removes the inline scripts alltogether.

You can install it with pip install django-cookie-consent --pre or by pinning the exact version. The relevant documentation can be found here: https://django-cookie-consent.readthedocs.io/en/latest/javascript.html

Can you let me know if this works for you and resolves the issue? If not, please let me know what issues you are still facing so we can tidy this up in a proper non-beta release!

sergei-maertens avatar Sep 24 '23 14:09 sergei-maertens

Closing this as resolved, the cookiebar module JS was designed with this ticket in mind.

sergei-maertens avatar May 09 '24 11:05 sergei-maertens