-
-
Notifications
You must be signed in to change notification settings - Fork 23
Deferred static files #77
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
Merged
Merged
Changes from 10 commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
51a473a
functional static_css implementation
Archmonger b1a2bcf
Bump Django IDOM version
Archmonger 6b82359
format components.py
Archmonger 551b6f1
rudimentary docs
Archmonger d5b0d9c
fix docstring
Archmonger 11b3e42
clean up readme
Archmonger e9339fa
static_js
Archmonger 836fe71
minor docs styling tweaks
Archmonger 0ee9402
Update docs/features/components.md
Archmonger a19508f
Merge branch 'static-css' of https://github.com/Archmonger/django-ido…
Archmonger 4cc86aa
flesh out the docs
Archmonger 526bc8f
simplify _cached_static_contents
Archmonger 6b0dfb8
add static js to changelog
Archmonger 70d78fc
clean up changelog entry
Archmonger 0ffb5a5
Selenium 4.3 compatibility
Archmonger 6cc9ded
add title to readme example
Archmonger 7733243
pin selenium to older version 4.2
Archmonger 568a49e
Revert "Selenium 4.3 compatibility"
Archmonger 5f981a0
add new tests
Archmonger f9aa39f
fix tests
Archmonger aa55ad7
docs cleanup
Archmonger fa11504
Create PR template
Archmonger 03ac980
bump the minimum idom version
Archmonger 7095f64
update issue form
Archmonger bdb3431
minor readme wordsmithing
Archmonger 7ea6306
docstring for component template tag
Archmonger 2de0dee
use PascalCase for component names
Archmonger 1db08a5
Fix formatting
Archmonger 1e26dfa
fix HR on JS test
Archmonger 53ea6e6
wordsmith
Archmonger ad33221
fix spelling
Archmonger 574acdb
Update tests/test_app/static/static-css-test.css
Archmonger aea5062
Update .github/ISSUE_TEMPLATE/issue-form.yml
Archmonger 18dc658
Update tests/test_app/static/static-js-test.js
Archmonger 2660882
use_memo is preferrable
Archmonger 4412459
Merge branch 'static-css' of https://github.com/Archmonger/django-ido…
Archmonger a544c11
use snake_case
Archmonger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
## Static CSS | ||
|
||
Allows you to defer loading a CSS stylesheet until a component begins rendering. This stylesheet must be stored within [Django's static files](https://docs.djangoproject.com/en/dev/howto/static-files/). | ||
|
||
```python title="components.py" | ||
from idom import component, html | ||
from django_idom.components import static_css | ||
|
||
@component | ||
def MyComponent(): | ||
return html.div( | ||
static_css("/static/css/buttons.css"), | ||
html.button("My Button!") | ||
) | ||
``` | ||
|
||
??? question "Should I put `static_css` at the top of my component?" | ||
|
||
<!-- Yes, to ensure proper load order --> | ||
|
||
??? question "Can I load HTML using `html.link`?" | ||
|
||
??? question "What about external stylesheets?" | ||
|
||
??? question "Why not load my CSS in `#!html <head>`?" | ||
|
||
<!-- Generally, Django stylesheets are loaded in your `#!html <head>` using the `#!jinja {% load static %}` template tag. --> | ||
|
||
## Static JavaScript | ||
|
||
<!-- In progress --> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
from . import hooks | ||
from . import components, hooks | ||
from .websocket.consumer import IdomWebsocket | ||
from .websocket.paths import IDOM_WEBSOCKET_PATH | ||
|
||
|
||
__version__ = "1.0.0" | ||
__all__ = ["IDOM_WEBSOCKET_PATH", "IdomWebsocket", "hooks"] | ||
__version__ = "1.1.0" | ||
__all__ = ["IDOM_WEBSOCKET_PATH", "IdomWebsocket", "hooks", "components"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import os | ||
|
||
from django.contrib.staticfiles.finders import find | ||
from idom import component, html | ||
|
||
from django_idom.config import IDOM_CACHE | ||
|
||
|
||
@component | ||
def static_css(static_path: str): | ||
"""Fetches a CSS static file for use within IDOM. This allows for deferred CSS loading.""" | ||
return html.style(_cached_static_contents(static_path, "css_contents")) | ||
|
||
|
||
@component | ||
def static_js(static_path: str): | ||
"""Fetches a JS static file for use within IDOM. This allows for deferred JS loading.""" | ||
return html.script(_cached_static_contents(static_path, "js_contents")) | ||
|
||
|
||
def _cached_static_contents(static_path: str, cache_name: str): | ||
# Try to find the file within Django's static files | ||
abs_path = find(static_path) | ||
if not abs_path: | ||
raise FileNotFoundError( | ||
f"Could not find static file {static_path} within Django's static files." | ||
) | ||
|
||
# Fetch the file from cache, if available | ||
last_modified_time = os.stat(abs_path).st_mtime | ||
cache_key = f"django_idom:{cache_name}:{static_path}" | ||
file_contents = IDOM_CACHE.get(cache_key, version=last_modified_time) | ||
Archmonger marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if file_contents is None: | ||
with open(abs_path, encoding="utf-8") as static_file: | ||
file_contents = static_file.read() | ||
IDOM_CACHE.delete(cache_key) | ||
IDOM_CACHE.set( | ||
cache_key, file_contents, timeout=None, version=last_modified_time | ||
) | ||
|
||
return file_contents |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.