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

Add kwargs support to vite_react_refresh #94

Closed
wants to merge 1 commit into from
Closed
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
18 changes: 14 additions & 4 deletions django_vite/templatetags/django_vite.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,20 +540,26 @@ def _generate_vite_server_url(path: str) -> str:
)

@classmethod
def generate_vite_react_refresh_url(cls) -> str:
def generate_vite_react_refresh_url(cls, **kwargs) -> str:
"""
Generates the script for the Vite React Refresh for HMR.
Only used in development, in production this method returns
an empty string.

Keyword Arguments:
**kwargs {Dict[str, str]} -- Adds new attributes to generated
script tags.

Returns:
str -- The script or an empty string.
"""

if not DJANGO_VITE_DEV_MODE:
return ""

return f"""<script type="module">
attrs_str = " ".join([f'{key}="{value}"' for key, value in kwargs.items()])

return f"""<script type="module" {attrs_str}>
import RefreshRuntime from \
'{cls._generate_vite_server_url(DJANGO_VITE_REACT_REFRESH_URL)}'
RefreshRuntime.injectIntoGlobalHook(window)
Expand Down Expand Up @@ -750,13 +756,17 @@ def vite_legacy_asset(

@register.simple_tag
@mark_safe
def vite_react_refresh() -> str:
def vite_react_refresh(**kwargs: Dict[str, str]) -> str:
"""
Generates the script for the Vite React Refresh for HMR.
Only used in development, in production this method returns
an empty string.

Keyword Arguments:
**kwargs {Dict[str, str]} -- Adds new attributes to generated
script tags.

Returns:
str -- The script or an empty string.
"""
return DjangoViteAssetLoader.generate_vite_react_refresh_url()
return DjangoViteAssetLoader.generate_vite_react_refresh_url(**kwargs)
15 changes: 15 additions & 0 deletions tests/tests/templatetags/test_vite_react_refresh.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,18 @@ def test_vite_react_refresh_url_setting(patch_settings):
soup = BeautifulSoup(html, "html.parser")
script_tag = soup.script
assert "http://localhost:3000/static/foobar" in script_tag.text


def test_vite_react_refresh_uses_kwargs(patch_settings):
patch_settings({"DJANGO_VITE_REACT_REFRESH_URL": "foobar"})
template = Template(
"""
{% load django_vite %}
{% vite_react_refresh nonce="woo-nonce" %}
"""
)
html = template.render(Context({}))
soup = BeautifulSoup(html, "html.parser")
script_tag = soup.script
assert script_tag.has_attr("nonce")
assert script_tag["nonce"] == "woo-nonce"