diff --git a/django_vite/apps.py b/django_vite/apps.py new file mode 100644 index 0000000..ab781bc --- /dev/null +++ b/django_vite/apps.py @@ -0,0 +1,39 @@ +from django.apps import AppConfig +from django.core.checks import Warning, register + +from .templatetags.django_vite import DjangoViteAssetLoader + + +class DjangoViteAppConfig(AppConfig): + name = "django_vite" + verbose_name = "Django Vite" + + def ready(self) -> None: + try: + # Create Loader instance at startup to prevent threading problems. + DjangoViteAssetLoader.instance() + except RuntimeError: + # Just continue, the system check below outputs a warning. + pass + + +@register +def check_loader_instance(**kwargs): + """Raise a warning during startup when instance retrieval fails.""" + + try: + # Make Loader instance at startup to prevent threading problems + DjangoViteAssetLoader.instance() + return [] + except RuntimeError as exception: + return [ + Warning( + exception, + id="DJANGO_VITE", + hint=( + "Make sure you have generated a manifest file, " + "and that the DJANGO_VITE_MANIFEST_PATH points " + "to the correct location." + ), + ) + ] diff --git a/django_vite/templatetags/django_vite.py b/django_vite/templatetags/django_vite.py index fcdb8c4..69999a8 100644 --- a/django_vite/templatetags/django_vite.py +++ b/django_vite/templatetags/django_vite.py @@ -466,10 +466,6 @@ def _generate_production_server_url(path: str) -> str: return urljoin(DJANGO_VITE_STATIC_URL_PREFIX, path) -# Make Loader instance at startup to prevent threading problems -DjangoViteAssetLoader.instance() - - @register.simple_tag @mark_safe def vite_hmr_client(**kwargs: Dict[str, str]) -> str: