Server-side rendering and hydration
Teloce-Py 0.3+Teloce-Py can render a component to HTML on the server and then hydrate it in the browser. This page shows how npm packages fit in.
The rule
Server-side rendering never runs your component's JavaScript. It does not call data(), methods or lifecycle hooks. It renders the template from the values you pass in. So:
- Packages are browser-only. Use them in
mounted(), which runs in the browser during hydration. - Do not call
use()indata(), in computed values or in templates of an SSR component. There is nouse()on the server. - Pass every field the template needs as server data. A package can then add to the page after it loads.
How the pieces fit
- The server renders HTML from your component and sends it, with the Teloce-NPM script tags in
<head>. - The browser shows the HTML immediately.
- The loader fetches the packages. Hydration attaches your component to the existing HTML.
mounted()runs, waits foruse.ready, and then uses the package.
The component
Use dayjs from teloce-npm
<template>
<main class="page">
<h1>{{ title }}</h1>
<p v-if="renderedAt" id="rendered-at">Hydrated in your browser at {{ renderedAt }}</p>
<ul>
<li v-for="(item, index) in items" :key="item.id">{{ index + 1 }}. {{ item.label }}</li>
</ul>
</main>
</template>
<script>
export default {
data() {
return { title: "Server-rendered Python UI", items: [], renderedAt: "" };
},
async mounted() {
await use.ready; // browser only, after hydration
this.renderedAt = use("dayjs")().format("HH:mm:ss");
}
};
</script>renderedAt starts empty, so the server renders without the line. The browser fills it in.
Configure Teloce-NPM
A server that builds its own page has no HTML file to inject into:
{
"entry_html": null,
"output_dir": "public/teloce-npm",
"base_path": "/teloce-npm"
}teloce-npm installThe host
This is the part of Teloce-Py's ssr-python example that changes. Teloce-NPM provides generate_script_tags for hosts that build the page in Python:
from teloce.build import build_project
from teloce.server import Renderer, SSRRequest
from teloce_npm.injector import generate_script_tags
NPM_DIR = ROOT / "public" / "teloce-npm" # written by `teloce-npm install`
def build() -> Renderer:
build_project(
ROOT,
DIST,
options={
"html_mode": True,
"source_roots": ["ui"], # scan only your components
"ssr": True,
"ssr_entries": ["ui/App.html"],
"clean": True,
# ... your other options
},
)
return Renderer(DIST, cache_size=32)
def page(renderer: Renderer, request_path: str) -> bytes:
result = renderer.render(
"ui/App.html",
{"title": "Server-rendered Python UI", "renderedAt": "",
"items": [{"id": "python", "label": "No Node.js required"}]},
request=SSRRequest(path=request_path),
)
document = (
'<!doctype html><html><head><meta charset="utf-8">'
+ generate_script_tags("/teloce-npm") # <- the Teloce-NPM tags
+ "".join(result.head)
+ '</head><body><div id="app" data-teloce-ssr="1">'
+ result.html
+ '</div><script type="module">import { hydrate } from ...; hydrate("#app", '
+ result.props_json
+ ");</script></body></html>"
)
return document.encode("utf-8")Your server must also serve public/teloce-npm/ at /teloce-npm/. With Flask, FastAPI, Flaxon or any static handler that is one mount. Take care that the path cannot escape the folder.
What I checked
I ran this on Teloce-Py's ssr-python example, with a package added:
| Check | Result |
|---|---|
| Raw server HTML (fetched without running JavaScript) | Contains the rendered content and the data-teloce-ssr marker |
| Teloce-NPM tags in the raw HTML | Present in <head> |
| The package-powered line in the raw HTML | Absent, as expected |
| After hydration in a browser | The line appears (Hydrated in your browser at 15:23:52), v-model still works, no console errors |
Troubleshooting
use is not definedoruse(...) is not a functioninmounted(). The hook ran before the packages loaded. Addawait use.ready, and check that the two script tags are in the server's HTML.- A render error about an unsupported expression. You called
use()(or another function) in the template or in data. Move it tomounted(). - The build tries to compile files in
.teloce-npm/. Setsource_rootsso only your components are scanned.