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

Fix bug where remote CSS and JS are not rendered correctly #38

Merged
merged 4 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 21 additions & 15 deletions src/jinjax/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,17 +205,23 @@ def irender(

component = Component(name=__name, url_prefix=url_prefix, source=source)

for css in component.css:
if root_path and self.fingerprint:
css = self._fingerprint(root_path, css)
if css not in self.collected_css:
self.collected_css.append(css)

for js in component.js:
if root_path and self.fingerprint:
js = self._fingerprint(root_path, js)
if js not in self.collected_js:
self.collected_js.append(js)
for url in component.css:
if not url.startswith(("http://", "https://")):
if root_path and self.fingerprint:
url = self._fingerprint(root_path, url)
url = f"{self.root_url}{url}"

if url not in self.collected_css:
self.collected_css.append(url)

for url in component.js:
if not url.startswith(("http://", "https://")):
if root_path and self.fingerprint:
url = self._fingerprint(root_path, url)
url = f"{self.root_url}{url}"

if url not in self.collected_js:
self.collected_js.append(url)

attrs = attrs.as_dict if isinstance(attrs, HTMLAttrs) else attrs
attrs.update(kw)
Expand Down Expand Up @@ -260,12 +266,12 @@ def get_source(self, cname: str, file_ext: "TFileExt" = "") -> str:

def render_assets(self, fingerprint: bool = False) -> str:
html_css = [
f'<link rel="stylesheet" href="{self.root_url}{css}">'
for css in self.collected_css
f'<link rel="stylesheet" href="{url}">'
for url in self.collected_css
]
html_js = [
f'<script type="module" src="{self.root_url}{js}"></script>'
for js in self.collected_js
f'<script type="module" src="{url}"></script>'
for url in self.collected_js
]
return Markup("\n".join(html_css + html_js))

Expand Down
12 changes: 5 additions & 7 deletions tests/test_render.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import time
from pathlib import Path

import pytest
<<<<<<< HEAD
import jinja2
=======
from pathlib import Path

>>>>>>> 281fd27 (Optional fingerprinting)
from jinja2.exceptions import TemplateSyntaxError

import jinjax
Expand Down Expand Up @@ -141,7 +137,7 @@ def test_render_assets(catalog, folder):
""")

(folder / "Card.jinja").write_text("""
{#css card.css #}
{#css https://somewhere.com/style.css, card.css #}
{#js card.js, shared.js #}
<section class="card">
{{ content }}
Expand All @@ -157,7 +153,7 @@ def test_render_assets(catalog, folder):

(folder / "Page.jinja").write_text("""
{#def message #}
{#js shared.js #}
{#js https://somewhere.com/blabla.js, shared.js #}
<Layout>
<Card>
<Greeting message={message} />
Expand All @@ -170,8 +166,10 @@ def test_render_assets(catalog, folder):
print(html)
assert """
<html>
<link rel="stylesheet" href="https://somewhere.com/style.css">
<link rel="stylesheet" href="/static/components/card.css">
<link rel="stylesheet" href="/static/components/greeting.css">
<script type="module" src="https://somewhere.com/blabla.js"></script>
<script type="module" src="/static/components/shared.js"></script>
<script type="module" src="/static/components/card.js"></script>
<script type="module" src="/static/components/greeting.js"></script>
Expand Down
Loading