diff --git a/src/djwc/apps.py b/src/djwc/apps.py index cee06bda3df2b08dee1742052113a9dc460f7897..dde297ef14209452e5bfe8e657edbaee6914c05f 100644 --- a/src/djwc/apps.py +++ b/src/djwc/apps.py @@ -1,3 +1,4 @@ +import copy import importlib from pathlib import Path @@ -18,8 +19,6 @@ class AppConfig(apps.AppConfig): self.components = dict() for name, config in apps.apps.app_configs.items(): - if 'polymer' in name: - breakpoint() self.components.update(getattr(config, 'components', {})) for lib in DJWC.get('LIBRARIES', []): @@ -28,6 +27,22 @@ class AppConfig(apps.AppConfig): if 'COMPONENTS' in DJWC: self.components.update(settings.DJWC['COMPONENTS']) + for tag, definitions in self.components.items(): + if not isinstance(definitions, (list, tuple)): + definitions = (definitions,) + + self.components[tag] = tuple( + definition + if isinstance(definition, dict) + else dict(src=definition) + for definition in definitions + ) + self.bcomponents = { - k.encode('utf8'): v for k, v in self.components.items() + tag.encode('utf8'): definitions + for tag, definitions in self.components.items() } + for tag, definition in self.bcomponents.items(): + for i in range(0, len(self.bcomponents[tag])): + self.bcomponents[tag][i]['src'] = \ + self.bcomponents[tag][i]['src'].encode('utf8') diff --git a/src/djwc/middleware.py b/src/djwc/middleware.py index b2d9cd6bc4f3b38df80deef15b4fc8df9429ba7a..70dcf2b69092527d74dcf8ea04434c35c039c047 100644 --- a/src/djwc/middleware.py +++ b/src/djwc/middleware.py @@ -24,18 +24,25 @@ class StaticMiddleware: print(f'Element {tag} not found in registry') continue - response.content = response.content.replace( - b'</head>', - b''.join([ - b'<script type="module">import ', - b"'", - self.app.bstatic, - self.app.bcomponents[tag].encode('utf8'), - b"'", - b'</script>', - b'</head>', - ]) - ) + for script in self.app.bcomponents[tag]: + src = script['src'] + if src in loaded: + continue + module = script.get('module', True) + response.content = response.content.replace( + b'</body>', + b''.join([ + b'<script ' + b'type="module"' if module else b'nomodule', + b' src="', + self.app.bstatic, + src, + b'">', + b'</script>', + b'</body>', + ]) + ) + loaded.append(src) loaded.append(tag) return response