diff --git a/infrastructure/fragments.py b/infrastructure/fragments.py index 2c287c2..699a539 100644 --- a/infrastructure/fragments.py +++ b/infrastructure/fragments.py @@ -59,6 +59,15 @@ def _internal_url(app_name: str) -> str: # Public API # ------------------------------------------------------------------ +def _is_fragment_request() -> bool: + """True when the current request is itself a fragment fetch.""" + try: + from quart import request as _req + return bool(_req.headers.get(FRAGMENT_HEADER)) + except Exception: + return False + + async def fetch_fragment( app_name: str, fragment_type: str, @@ -72,7 +81,13 @@ async def fetch_fragment( Returns the raw HTML string. When *required* is True (default), raises ``FragmentError`` on network errors or non-200 responses. When *required* is False, returns ``""`` on failure. + + Automatically returns ``""`` when called inside a fragment request + to prevent circular dependencies between apps. """ + if _is_fragment_request(): + return "" + base = _internal_url(app_name) url = f"{base}/internal/fragments/{fragment_type}" try: