Skip to content

Allow django_css and django_js to load only_once #194

Closed as not planned
Closed as not planned

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)

Activity

  1. Archmonger commented on Jan 11, 2024

    @Archmonger
    ContributorAuthor

    Note to self: Storing the stylesheet within the scope will still result in duplicate stylesheets if multiple root components exist on the page.

    Loading the stylesheets might need to be handled via JavaScript and somehow globally track whether a stylesheet is needed.

    Or thinking in reverse, perhaps we should always load the stylesheet every time, but have duplicates get deleted via JavaScript?


    Another idea is using a global JavaScript object to keep track of how many components want a certain stylesheet.

    The stylesheet themselves would need to be stored in some master object, and would need to get unloaded after there are no component left that use the stylesheet.

    On the page, this master object might look like this:

    <body>
        <div id="reactpy-stylesheets">
            <style id="path/to/stylesheet.css"> ... </style>
        </div>
    </body>
  2. linked a pull request that will close this issueAdd Django CSS/JS de-duplication #226on Feb 13, 2024
  3. Archmonger commented on Dec 25, 2024

    @Archmonger
    ContributorAuthor

    Closing due to reasons described here:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions