Skip to content

Commit

Permalink
Merge pull request #75 from thijskramer/initialize-loader-instance-di…
Browse files Browse the repository at this point in the history
…fferently

Create singleton instance on app ready
  • Loading branch information
thijskramer authored Jun 1, 2023
2 parents 2b5f899 + 6800d68 commit bb596c5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
39 changes: 39 additions & 0 deletions django_vite/apps.py
Original file line number Diff line number Diff line change
@@ -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."
),
)
]
4 changes: 0 additions & 4 deletions django_vite/templatetags/django_vite.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit bb596c5

Please sign in to comment.