diff --git a/README.md b/README.md index 31d8f1b..9ef623b 100644 --- a/README.md +++ b/README.md @@ -183,7 +183,13 @@ like the previous tag.** ``` {% vite_react_refresh %} ``` -If you're using React, this will generate the Javascript needed to support React HMR. +If you're using React, this will generate the Javascript `' @@ -54,6 +63,6 @@ def stylesheet_preload(href: str) -> Tag: @staticmethod def preload(href: str, attrs: Dict[str, str]) -> Tag: - attrs_str = " ".join([f'{key}="{value}"' for key, value in attrs.items()]) + attrs_str = attrs_to_str(attrs) return f'' diff --git a/django_vite/templatetags/django_vite.py b/django_vite/templatetags/django_vite.py index f10d395..33aab81 100644 --- a/django_vite/templatetags/django_vite.py +++ b/django_vite/templatetags/django_vite.py @@ -178,13 +178,22 @@ def vite_legacy_asset( @register.simple_tag @mark_safe -def vite_react_refresh(app: str = DEFAULT_APP_NAME) -> str: +def vite_react_refresh( + app: str = DEFAULT_APP_NAME, + **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.instance().generate_vite_react_refresh_url(app) + return DjangoViteAssetLoader.instance().generate_vite_react_refresh_url( + app, **kwargs + ) diff --git a/tests/tests/templatetags/test_vite_react_refresh.py b/tests/tests/templatetags/test_vite_react_refresh.py index aa82cf6..c9058bb 100644 --- a/tests/tests/templatetags/test_vite_react_refresh.py +++ b/tests/tests/templatetags/test_vite_react_refresh.py @@ -106,3 +106,35 @@ def test_vite_react_refresh_url_setting(patch_settings): soup = BeautifulSoup(html, "html.parser") script_tag = soup.script assert "http://localhost:5173/static/foobar" in script_tag.text + + +@pytest.mark.parametrize( + "patch_settings", + [ + { + "DJANGO_VITE_DEV_MODE": True, + "DJANGO_VITE_REACT_REFRESH_URL": "foobar", + }, + { + "DJANGO_VITE": { + "default": { + "dev_mode": True, + "react_refresh_url": "foobar", + } + } + }, + ], + indirect=True, +) +def test_vite_react_refresh_uses_kwargs(patch_settings): + 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"