Skip to content

Experiment/vue ssr hydration - #83

Draft
evnchn wants to merge 1 commit into
mainfrom
experiment/vue-ssr-hydration
Draft

evnchn wants to merge 1 commit into
mainfrom
experiment/vue-ssr-hydration

Conversation

@evnchn

@evnchn evnchn commented Feb 14, 2026 •

Copy link
Copy Markdown
Owner

What this branch does

Adds Vue server-side rendering (SSR) to NiceGUI using MiniRacer (a V8 engine embedded in Python) to pre-render Vue component HTML on the server. The SSR HTML is injected into the #app div so the browser paints content immediately before JavaScript loads and Vue hydrates, dramatically improving time-to-visible-content. The branch evolved through several iterations: starting with Vue's createSSRApp hydration, then discovering Quasar UMD incompatibilities, and ultimately building a custom Quasar ESM bundle with __QUASAR_SSR_CLIENT__=true to enable proper hydration with isRuntimeSsrPreHydration mode.

Key changes

  • nicegui/ssr.py: Core SSR module -- renders Vue components in MiniRacer V8 with polyfills, postprocesses HTML (strips number input values, fixes nested <a> tags)
  • nicegui/client.py: Integrates SSR rendering into the page response pipeline
  • nicegui/static/ssr-entry.js, nicegui/static/ssr-polyfills.js, nicegui/static/ssr-bundle.js: SSR JavaScript infrastructure (esbuild-bundled IIFE for MiniRacer)
  • nicegui/static/quasar.esm.js / quasar.esm.prod.js: Custom ESM Quasar builds with SSR client flags enabled
  • nicegui/static/nicegui.js: Detects SSR HTML presence via innerHTML.trim() and uses createSSRApp vs createApp accordingly
  • nicegui/templates/index.html: Updated to support SSR HTML injection and ESM Quasar loading via import maps
  • extract_core_libraries.py: Build script updated for custom Quasar ESM build
  • tests/test_ssr.py: Unit tests for SSR postprocessing (number input stripping, anchor nesting fixes)
  • nicegui/testing/screen.py: Hydration mismatch allowance for test fixtures

Status

Experimental. This is a research branch exploring SSR/hydration for NiceGUI. It works but has known limitations: hydration mismatches are tolerated for viewport dimensions and crypto UUIDs, the MiniRacer dependency is heavy, and the custom Quasar ESM build adds build complexity. The branch has 12 commits reflecting an iterative discovery process.

Upstream PR

None yet

@evnchn
evnchn marked this pull request as draft February 14, 2026 16:39
falkoschindler added a commit that referenced this pull request Mar 12, 2026
### Motivation

This PR addresses multiple Dependabot security alerts by bumping
vulnerable dependencies to their patched versions:

- [#67](https://github.com/zauberzeug/nicegui/security/dependabot/67) -
`lodash-es` for vanilla-jsoneditor (json_editor element)
- [#68](https://github.com/zauberzeug/nicegui/security/dependabot/68) -
`fonttools` for matplotlib
- [#72](https://github.com/zauberzeug/nicegui/security/dependabot/72) -
`aiohttp` ≥3.13.3
- [#80](https://github.com/zauberzeug/nicegui/security/dependabot/80) -
`urllib3` ≥2.6.3
- [#83](https://github.com/zauberzeug/nicegui/security/dependabot/83) -
`orjson` ≥3.11.5

### Implementation

- Updated pyproject.toml with new minimum versions for `aiohttp`,
`orjson`, `urllib3`, and added `fonttools` to the matplotlib extra
- Pinned `lodash-es` ^4.17.23 in package.json for two elements to
override vulnerable transitive dependencies
- Regenerated package-lock.json files and uv.lock (`npm run build`
didn't change anything in the generated bundles)

### Progress

- [x] I chose a meaningful title that completes the sentence: "If
applied, this PR will..."
- [x] The implementation is complete.
- [x] This is not a security issue in NiceGUI itself, but updating
dependencies with already-public CVEs.
- [x] Pytests are not necessary.
- [x] Documentation is not necessary.
@evnchn evnchn added experimental Experiment/POC - preserve for reference javascript Pull requests that update javascript code labels Mar 29, 2026
@evnchn evnchn added doable:low Workable but heavy — rebase/review cost high banger-source Source of strong ideas worth mining even if PR itself does not land labels Apr 14, 2026
Pre-render Vue element tree server-side via MiniRacer and inject into
#app so the browser paints immediately; the full Vue app then hydrates
on top. Squash of 10 commits rebased onto current upstream/main.

Includes:
- nicegui/ssr.py: MiniRacer-backed server-side Vue render (render_to_string)
- Custom Quasar ESM build with SSR client flags; shared Vue/Quasar UMD
  via importmap so server + client use the same bundle
- #app ssr_html injection marker with HTML-escaping hooks
- Hydration fixes: number input, nested <a> tag sanitization
- mini-racer as dev dep; SSR-aware caplog filtering in tests
- tests/test_ssr.py (13 tests, all passing)

Lints: ruff/autopep8/mypy/pylint all clean; pre-commit green.
@evnchn
evnchn force-pushed the experiment/vue-ssr-hydration branch from ea7adbb to 33b3e92 Compare April 14, 2026 18:00
@evnchn

evnchn commented Apr 14, 2026

Copy link
Copy Markdown
Owner Author

Rebased onto current upstream/main (was 125 behind). Fork-only push, no upstream PR.

Rebase: clean after 2 mechanical conflicts in nicegui/templates/index.html (both involved upstream's new #esm-fallback block coexisting with SSR's #app ssr_html injection and Quasar ESM's qmeta-init script — all additive, no semantic collision). Squashed 10 commits into one.

Lints (all green)

  • pre-commit (ruff, autopep8, codespell, mdformat): PASS
  • mypy: Success, no issues found in 237 source files
  • pylint: 10.00/10
  • tests/test_ssr.py: 13/13 passed

Mineable ideas (banger-source)

Even if this PR never lands, these techniques are individually cherry-pickable:

  • MiniRacer for server-side Vue rendering — embed V8 in the Python process to run Vue's SSR renderer without a Node sidecar. Sidesteps the subprocess/IPC complexity that usually kills Python+JS SSR. Pattern is generalizable to any "run small bit of JS server-side" need (syntax highlighters, markdown renderers, validation schemas).
  • Quasar UMD → custom ESM build with SSR client flags — rebuilding Quasar as ESM lets you share the same bundle between server pre-render and client hydration via importmap. Eliminates the "two bundles drift" class of SSR bugs. Worth considering independently of SSR for faster cold starts.
  • #app ssr_html injection marker — keeping the injection point as a single {{ ssr_html | safe }} inside the existing #app div (rather than a sibling or shadow tree) means Vue's createApp(...).mount("#app") naturally hydrates the pre-rendered content. Minimal template surface area.
  • Hydration-safe HTML escaping — the HTML_ESCAPE_TABLE str.maketrans approach in client.py for pre-render output is cleaner and faster than html.escape for hot paths. Worth factoring out.
  • Nested <a> tag sanitization during SSR — browsers silently reparse nested anchors, which breaks Vue hydration (DOM doesn't match vnode tree). The fix pattern (detect + flatten before serializing) is a gotcha every SSR implementation rediscovers.
  • Number input SSR hydration fix — Vue's default number input serialization diverges between server and client; the targeted fix is a template for catching other form-element hydration mismatches.
  • SSR-aware caplog filtering in tests — keeping SSR warnings out of test records without suppressing them globally is a nice pattern for any "expected noisy subsystem" in pytest.

Artifact preserved at https://github.com/evnchn/nicegui/tree/experiment/vue-ssr-hydration — single squashed commit on top of upstream/main.

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

Labels

banger-source Source of strong ideas worth mining even if PR itself does not land doable:low Workable but heavy — rebase/review cost high experimental Experiment/POC - preserve for reference javascript Pull requests that update javascript code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant