v0.165.0
 1from plain.runtime import settings
 2from plain.urls import Router, path, reverse
 3
 4from .manifest import get_manifest
 5from .views import AssetView
 6
 7
 8class AssetsRouter(Router):
 9    """
10    The router for serving static assets.
11
12    Include this router in your app router if you are serving assets yourself.
13    """
14
15    namespace = "assets"
16    urls = (path("<path:path>", AssetView, name="asset"),)
17
18
19def get_asset_url(url_path: str) -> str:
20    """Get the full URL to a given asset path."""
21    # In debug mode, always use the original URL path.
22    # In production, use compiled URL path if available (may be fingerprinted).
23    if settings.DEBUG:
24        resolved_url_path = url_path
25    else:
26        resolved_url_path = get_manifest().resolve(url_path) or url_path
27
28    if settings.ASSETS_CDN_URL:
29        return f"{settings.ASSETS_CDN_URL.rstrip('/')}/{resolved_url_path.lstrip('/')}"
30
31    return reverse(f"{AssetsRouter.namespace}:asset", path=resolved_url_path)