Skip to content

Add Django CSS/JS de-duplication #226

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

Closed
Closed
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Only configure the scope if using allow_duplicates=false
  • Loading branch information
Archmonger committed Feb 12, 2024
commit dcd6a55d859a815100ed5eff2d7abf2a2a78d8f7
16 changes: 12 additions & 4 deletions src/reactpy_django/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,12 +259,16 @@ def _view_to_iframe(
def _django_css(static_path: str, allow_duplicates: bool):
scope = use_scope()
ownership_uuid = hooks.use_memo(lambda: uuid4())
scope.setdefault("reactpy", {}).setdefault("css", {})
scope["reactpy"]["css"].setdefault(static_path, ownership_uuid)

# Configure the scope to track the file
if not allow_duplicates:
scope.setdefault("reactpy", {}).setdefault("css", {})
scope["reactpy"]["css"].setdefault(static_path, ownership_uuid)

# Load the file if no other component has loaded it
@hooks.use_effect(dependencies=None)
async def duplicate_manager():
"""Note: This hook runs on every render. This is intentional."""
if allow_duplicates:
return

Expand All @@ -287,12 +291,16 @@ def unmount():
def _django_js(static_path: str, allow_duplicates: bool):
scope = use_scope()
ownership_uuid = hooks.use_memo(lambda: uuid4())
scope.setdefault("reactpy", {}).setdefault("js", {})
scope["reactpy"]["js"].setdefault(static_path, ownership_uuid)

# Configure the scope to track the file
if not allow_duplicates:
scope.setdefault("reactpy", {}).setdefault("js", {})
scope["reactpy"]["js"].setdefault(static_path, ownership_uuid)

# Load the file if no other component has loaded it
@hooks.use_effect(dependencies=None)
async def duplicate_manager():
"""Note: This hook runs on every render. This is intentional."""
if allow_duplicates:
return

Expand Down