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

Use staticfiles.storage to generate URLs #52

Merged
merged 1 commit into from
May 22, 2023
Merged
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
47 changes: 40 additions & 7 deletions django_vite/templatetags/django_vite.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from urllib.parse import urljoin

from django import template
from django.apps import apps
from django.conf import settings
from django.utils.safestring import mark_safe

Expand Down Expand Up @@ -138,7 +139,9 @@ def generate_vite_asset(
# Add the script by itself
tags.append(
DjangoViteAssetLoader._generate_script_tag(
urljoin(DJANGO_VITE_STATIC_URL, manifest_entry["file"]),
DjangoViteAssetLoader._generate_production_server_url(
manifest_entry["file"]
),
attrs=scripts_attrs,
)
)
Expand Down Expand Up @@ -173,11 +176,14 @@ def _generate_css_files_of_asset(
if "css" in manifest_entry:
for css_path in manifest_entry["css"]:
if css_path not in already_processed:
tags.append(
DjangoViteAssetLoader._generate_stylesheet_tag(
urljoin(DJANGO_VITE_STATIC_URL, css_path)
url = (
DjangoViteAssetLoader._generate_production_server_url(
css_path
)
)
tags.append(
DjangoViteAssetLoader._generate_stylesheet_tag(url)
)

already_processed.append(css_path)

Expand Down Expand Up @@ -208,7 +214,9 @@ def generate_vite_asset_url(self, path: str) -> str:
f"at {DJANGO_VITE_MANIFEST_PATH}"
)

return urljoin(DJANGO_VITE_STATIC_URL, self._manifest[path]["file"])
return DjangoViteAssetLoader._generate_production_server_url(
self._manifest[path]["file"]
)

def generate_vite_legacy_polyfills(
self,
Expand Down Expand Up @@ -240,7 +248,9 @@ def generate_vite_legacy_polyfills(
for path, content in self._manifest.items():
if DJANGO_VITE_LEGACY_POLYFILLS_MOTIF in path:
return DjangoViteAssetLoader._generate_script_tag(
urljoin(DJANGO_VITE_STATIC_URL, content["file"]),
DjangoViteAssetLoader._generate_production_server_url(
content["file"]
),
attrs=scripts_attrs,
)

Expand Down Expand Up @@ -288,7 +298,9 @@ def generate_vite_legacy_asset(
scripts_attrs = {"nomodule": "", "crossorigin": "", **kwargs}

return DjangoViteAssetLoader._generate_script_tag(
urljoin(DJANGO_VITE_STATIC_URL, manifest_entry["file"]),
DjangoViteAssetLoader._generate_production_server_url(
manifest_entry["file"]
),
attrs=scripts_attrs,
)

Expand Down Expand Up @@ -428,6 +440,27 @@ def generate_vite_react_refresh_url(cls) -> str:
window.__vite_plugin_react_preamble_installed__ = true
</script>"""

@staticmethod
def _generate_production_server_url(path: str) -> str:
"""
Generates an URL to an asset served during production.

Keyword Arguments:
path {str} -- Path to the asset.

Returns:
str -- Full URL to the asset.
"""

if apps.is_installed("django.contrib.staticfiles"):
from django.contrib.staticfiles.storage import staticfiles_storage

return staticfiles_storage.url(
urljoin(DJANGO_VITE_STATIC_URL_PREFIX, path)
)
else:
return urljoin(DJANGO_VITE_STATIC_URL_PREFIX, path)


# Make Loader instance at startup to prevent threading problems
DjangoViteAssetLoader.instance()
Expand Down