v0.142.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 = [
17        path("<path:path>", AssetView, name="asset"),
18    ]
19
20
21def get_asset_url(url_path: str) -> str:
22    """Get the full URL to a given asset path."""
23    # In debug mode, always use the original URL path.
24    # In production, use compiled URL path if available (may be fingerprinted).
25    if settings.DEBUG:
26        resolved_url_path = url_path
27    else:
28        resolved_url_path = get_manifest().resolve(url_path) or url_path
29
30    if settings.ASSETS_CDN_URL:
31        return f"{settings.ASSETS_CDN_URL.rstrip('/')}/{resolved_url_path.lstrip('/')}"
32
33    return reverse(f"{AssetsRouter.namespace}:asset", path=resolved_url_path)