Closed rbeard0330 closed 3 years ago
I think your suggestion would work well, and would remove the TemplateResponse
restriction. I think we can use the component name as the key, since that needs to be unique anyways. So the flow would be:
render_dependencies
tags also check whether the middleware is active. If not, they just render dependencies for all registered components (as they do currently). If it is, they render a placeholder. I think this should work, and it should also work in isolated contexts. I'll get to work on implementing and addressing your comments. I'll also do a before-and-after bench to see if there's an impact on performance. If there is a significant performance hit, we could use the TemplateResponse
approach as the default, and then fall back to the text-based solution for HttpResponse
s. That's more complexity though.
@rbeard0330 Thank you for working on this! Great that you'll look at performance too, that was a slight worry for me too.
@rbeard0330 Not to rush you, but will you be working on this in the near future? I have some fixes around adding black formatting to the code base, but I'd hate to make your code hard to merge because of it. Let me know if there's something I can do to help!
Sorry, I ran into some headwinds and then got distracted. I was able to get a new version together though, and just committed it. Let me know what you think once you've had a chance to review.
@rbeard0330 Thank you! There are a few unanswered questions above. Would you mind going through them and either answer them or make a commit and resolve them? It's a little tricky to know what change fixed what comment right now.
Sure. I'll run through those today.
Thank you so much! I made a copy of this PR in #52 without the first two commits in your PR. I also fixes some flake8 errors, which strangely does not get picked up my running tests on GitHub Actions.
As promised in #29. I wasn't able to find a way to make this work well using template tags alone. The problem is that CSS dependencies especially are rendered before most or all of the components, and there's not a great way to know what components will be rendered at that time. Django-sekizai deals with this problem by effectively having the first tag that needs to write dynamic content effectively hijack the parser, and read in the rest of the entire template. This allows it to render everything else first, see what happens, and then write its dynamic content at the right place in the output.
I didn't follow that approach since: 1) if people want to use sekizai to manage JS/CSS dependencies, they already can, and 2) that approach is pretty drastic and might break things in some cases. Instead, the approach here is to use a middleware to post-process the fully rendered response with the necessary dependencies. More specifically:
Limitations:
only
behavior of thecomponent
andcomponent_block
tags to still pass through the dependency tracker.Still to do:
Look forward to getting your thoughts!