django-components: Component does not load js file, when "-" or "/" in register name

Given is following code:

@component.register("foo-card")
class Foo(component.Component):
      template_name = "foo-card/foo-card.html"

      def get_context_data(self, url):
          return {
              "url": url,
          }

      class Media:
         js = "foo-card/foo-card.js"

The component is registered with the name “foo-card”. When this component is used the js file foo-card.js does not load!

When i change the register name of the component to:

@component.register("foocard")

the component and the js file foo-card.js load!

I think the js should also load when there is a - or / in the component name.

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 3
  • Comments: 16 (7 by maintainers)

Commits related to this issue

Most upvoted comments

Ah-ha, we have a culprit! This issue is entirely unrelated to what I assumed it would be. It’s ComponentDependencyMiddleware that we need to take a closer look at. (I’m new to this one. Took me a while to figure I need to set render_dependencies=True in addition to including the middleware itself in order for it to even run. Bit strange, that.) Anyway, now I can repro the error, we’re one step closer to figuring out the actual cause 🕵️‍♀️

Got it. The regexes in middleware.py are too strict. Adding a hyphen and / to it seems to make the issue go away.

Will revisit this tomorrow. Once I’m satisified that making the regexes more general doesn’t cause problems elsewhere, I’ll open a PR.

Excellent suggestion. I was so wrapped up in finding a fix it didn’t even occur to me to ask about the version. Will go back to the v0.25 commit to check if there’s a pattern.

@lemontheme Thanks for the detailed explanation, it would be cool if you can fix it, since I go tomorrow into holidays. But next time, if I find a bug, I will accept the challenge. 😄