Skip to content

Conversation

@evnchn
Copy link
Collaborator

@evnchn evnchn commented Nov 23, 2025

Motivation

TL-DR: dependency.py isn't coded as tightly as it should, silently letting (possibly critical) errors slide into client-side instead of stopping them right away and preventing the server from launching in a verbose and attention-grabbing manner.

I was working on #5493 and looking for space savings, when I found some interesting behaviour with regards to our dependency.py implementation.

  1. We set key for imports at 2 places, libraries and ESM modules

imports[library.name] = f'{prefix}/_nicegui/{__version__}/libraries/{key}'
done_libraries.add(key)
# build the importmap structure for ESM modules
for key, esm_module in esm_modules.items():
imports[f'{esm_module.name}'] = f'{prefix}/_nicegui/{__version__}/esm/{key}/index.js'
imports[f'{esm_module.name}/'] = f'{prefix}/_nicegui/{__version__}/esm/{key}/'

If ESM modules are to have the same name as a library, the library would be shadowed.

  1. We append JS imports into js_imports at 2 places, one for .js components, one for .vue components

js_imports.append(f'import {{ default as {vue_component.name} }} from "{url}";')
js_imports.append(f"{vue_component.name}.template = '#tpl-{vue_component.name}';")
js_imports.append(f'app.component("{vue_component.tag}", {vue_component.name});')
vue_styles.append(vue_component.style)
done_components.add(key)
# build the resources associated with the elements
for element in elements:
if element.component:
js_component = element.component
if js_component.key not in done_components and js_component.path.suffix.lower() == '.js':
url = f'{prefix}/_nicegui/{__version__}/components/{js_component.key}'
js_imports.append(f'import {{ default as {js_component.name} }} from "{url}";')
js_imports.append(f'app.component("{js_component.tag}", {js_component.name});')
js_imports_urls.append(url)

This one is a bit more serious: If you run import twice with same name, browser JS errors out entirely, and the page grinds to a halt (white screen)

Uncaught SyntaxError: Identifier 'BLAH' has already been declared

Implementation

  • 2 sets: import_names and component_names
  • Do assertations before registration
  • Update the set before returning

Progress

  • I chose a meaningful title that completes the sentence: "If applied, this PR will..."
  • The implementation is complete.
  • Pytests have been added (or are not necessary).
  • Documentation has been added (or is not necessary).

Final notes

May break existing code which works (barely) in case 1, but I think we should break it, because the code isn't valid in the first place.

@evnchn
Copy link
Collaborator Author

evnchn commented Nov 23, 2025

And if we unique-key by name, instead of unique-key by hash-based key, we get maximum savings in the following image (hence why I thought of this when working on #5493:

image

@evnchn evnchn added the bug Type/scope: A problem that needs fixing label Nov 23, 2025
@falkoschindler falkoschindler added the review Status: PR is open and needs review label Nov 24, 2025
@falkoschindler falkoschindler added this to the 3.5 milestone Nov 24, 2025
@evnchn
Copy link
Collaborator Author

evnchn commented Nov 24, 2025

No I think we should test this, in retrospect.

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

Labels

bug Type/scope: A problem that needs fixing review Status: PR is open and needs review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants