Skip to content

Commit

Permalink
fix: compatibility with Whitenoise compression
Browse files Browse the repository at this point in the history
BREAKING-CHANGE:
Assets now require the relative path you would normally use inside a static template tag
  • Loading branch information
SaschaKrug committed Jan 26, 2024
1 parent 383a2c1 commit 884a32e
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 89 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## [0.4.0] - 2024-01-26
fix: compatibility with Whitenoise compression

BREAKING-CHANGE:
Assets now require the relative path you would normally use inside a static template tag

## [0.3.1] - 2024-01-09
- Cache rendered assets

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,7 @@ from django_component_kit import component_inline_tag
register = template.Library()

@register.tag
@component_inline_tag(get_template("mycomponents/card.html"), js=["myjs.js"])
@component_inline_tag(get_template("mycomponents/card.html"), js=["myapp/myjs.js"])
def card() -> dict:
return dict()
```
Expand All @@ -720,7 +720,7 @@ def card() -> dict:

### Result
```html
<script src="myjs.js" defer></script>
<script src="static/myapp/myjs.js" defer></script>
```

# Contribution
Expand Down
5 changes: 3 additions & 2 deletions django_component_kit/assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Assets management for Django Component Kit.
"""
from typing import Iterable
from django.templatetags.static import static


class AssetRegistry:
Expand All @@ -15,12 +16,12 @@ def __init__(self):
def add_js(self, js_list: Iterable[str] | None):
"""Add JS files"""
js_list = js_list or []
self.js.update([f'<script src="{js}" defer></script>' for js in js_list])
self.js.update([f'<script src="{static(js)}" defer></script>' for js in js_list])

def add_css(self, css_list: Iterable[str] | None):
"""Add CSS files"""
css_list = css_list or []
self.css.update([f'<link crossorigin="anonymous" href="{css}" rel="stylesheet">' for css in css_list])
self.css.update([f'<link crossorigin="anonymous" href="{static(css)}" rel="stylesheet">' for css in css_list])

def as_string(self, refresh: bool = False) -> str:
"""Return cached assets as string"""
Expand Down
Loading

0 comments on commit 884a32e

Please sign in to comment.