Skip to content

Allow django_css and django_js to load only_once #194

Closed as not planned
Closed as not planned
@Archmonger

Description

@Archmonger

Current Situation

Currently, a user can add django_css to the body of a component that is rendered multiple times. This means that the CSS stylesheet will be duplicated for each component that is on the page.

Proposed Actions

Add an only_once = True argument to django_css and django_js. This will prevent CSS/JS duplication if the static file has already been loaded within the current component tree.

To detect whether a static file has been loaded, we should store whether a static_file_path has already been loaded within the ASGI scope. Here's some pseudocode on the concept:

# This should be initialized within the ReactPy WebSocket consumer
scope["reactpy_static_files"] = set()

# This check should occur within the `django_css` and `django_js` components
if static_file_path not in scope["reactpy_static_files"]:
   return get_static_file_contents(static_file_path)

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions