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

Create singleton instance on app ready #75

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
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