django-components icon indicating copy to clipboard operation
django-components copied to clipboard

Documentation: middleware.ComponentDependencyMiddleware not documented

Open EmilStenstrom opened this issue 3 years ago • 6 comments

You need to add 'django_components.middleware.ComponentDependencyMiddleware' to your MIDDLEWARE section too, that's the thing that makes smarter choices of where to import CSS and JS.

Originally posted by @EmilStenstrom in https://github.com/EmilStenstrom/django-components/discussions/70#discussioncomment-880635

EmilStenstrom avatar Jun 16 '21 22:06 EmilStenstrom

@rbeard0330 Do you think you could have a stab at this one?

EmilStenstrom avatar Sep 09 '21 20:09 EmilStenstrom

Will do.

rbeard0330 avatar Sep 10 '21 14:09 rbeard0330

I dont want to cut in on what @rbeard0330 is doing but if someone can do a quick loom and show me I dont mind taking a stab at the documentation

I would like to be a user of this feature

simkimsia avatar Jul 19 '22 02:07 simkimsia

@simkimsia I'm trying to remember here, was a couple of years since I played with this. I think you might just add this to your settings.py:

MIDDLEWARE = [
    ...
    'django_components.middleware.ComponentDependencyMiddleware'
]

... and things magically work?

EmilStenstrom avatar Jul 19 '22 07:07 EmilStenstrom

@EmilStenstrom i did and i got this error

ModuleNotFoundError: No module named 'django_components.middleware.ComponentDependencyMiddlewaredjango'; 'django_components.middleware' is not a package

I suspect need to change the __init__.py?

I am using 0.22 django-components by the way.

simkimsia avatar Jul 23 '22 06:07 simkimsia

Note the last part of the error message: "ComponentDependencyMiddlewaredjango". This means you forgot the comma after your middleware line, so it concatenated that string with the previous one. Try again, with a comma!

EmilStenstrom avatar Jul 26 '22 18:07 EmilStenstrom

Out of interest, would this allow Django to only load Media on pages that the components are used on? This would be ideal so I'm not loading JS for components that aren't used on the page.

timothyis avatar Feb 27 '23 13:02 timothyis

@timothyis yes, this is what this middleware does. It’s already in the code, just not documented.

EmilStenstrom avatar Feb 27 '23 16:02 EmilStenstrom

There's two steps needed to enable the middleware: Add the Middleware (as above) and setting the RENDER_DEPENDENCIES key to True in settings.

EmilStenstrom avatar Sep 03 '23 09:09 EmilStenstrom

Fixed by #360

EmilStenstrom avatar Jan 18 '24 08:01 EmilStenstrom