Skip to content

Keep server-rendered doc pages through hydration - #851

Merged
mishig25 merged 1 commit into
mainfrom
fix/hydration-safe-doc-components
Oct 13, 2026
Merged

mishig25 merged 1 commit into
mainfrom
fix/hydration-safe-doc-components

Conversation

@mishig25

Copy link
Copy Markdown
Contributor

Problem

Every API reference page on huggingface.co fails hydration. Svelte throws away the server-rendered HTML and re-renders the whole page in the browser. On transformers/main/en/model_doc/llama, 0 of 2,341 server-rendered elements survive. Every API page I sampled is affected (bert, qwen2, whisper, trainer, pipelines, generation_utils), while guide pages are not.

The cause is invalid HTML nesting in docstrings. The browser parses it into a different tree than the one Svelte expects, which shows up as hydration_mismatch:

  • Docstring.svelte wraps return and raise descriptions, which contain <p> elements, in a <p>. The browser closes the outer <p> early.
  • docstring.js strips the outer <p>…</p> of every parameter description. For a multi-paragraph description that leaves a stray </p><p> inside a <span>.

This also blocks translating the built HTML (the approach being explored as an alternative to #813): anything that changes the server HTML is replaced by the English re-render.

Changes

  • Docstring nesting: return and raise descriptions get a <div> wrapper instead of a <p>. A parameter description now has only its first paragraph unwrapped, so the first line still renders inline next to the parameter name.
  • Text props: Heading.svelte and the Docstring returns label and group titles render their text with {@html escapeHtml(...)}. During hydration Svelte keeps the server HTML of an {@html} block, but overwrites a plain {text} node with the prop value.
  • HfOption.svelte: renders every option and hides the inactive ones with hidden, instead of rendering only the selected one. All options' content is now in the HTML.
  • Stray scripts: docstring.js drops the <script context="module"> metadata block that mdsvex prepends to every docstring fragment. These ended up as inline scripts in the page (the Unexpected token 'export' console errors).

Testing

I built a small docs site with doc-builder build --html, once from main and once from this branch. It has a heading, an <hfoptions> block, and an autodoc'd function with a multi-paragraph parameter, Returns and Raises. I served each build the way moon-landing does: the fragment injected into a page, with _app served from the build. A probe tagged every server-rendered element before the SvelteKit bootstrap ran.

server elements kept after hydration edited (translated) strings that survive
main 0 / 134 0 / 7
this branch 130 / 130 7 / 7
  • The second hfoptions tab works when clicked and when loaded from ?backend=….
  • The docstring layout matches main.
  • npm run lint passes. npm run check reports 0 errors and the same 46 warnings as main.
  • On real Transformers pages served by a local moon-landing, the simulated version of the docstring fix hydrates model_doc/llama with 0 mismatches.

Notes

  • Inactive hfoptions content is now in the page HTML. Pages get larger, and the Meilisearch crawl will index every option rather than only the first.
  • The vertical spacing around return descriptions changes slightly, because the browser no longer inserts an implicit empty <p> there.

🤖 Generated with Claude Code

Every API reference page fails hydration today: Svelte discards the
server-rendered HTML and re-renders the whole page in the browser. The cause is
invalid HTML nesting in docstrings, which the browser parses into a different
tree than Svelte expects:

- Docstring.svelte wrapped return/raise descriptions, which contain <p>
  elements, in a <p>.
- docstring.js stripped the outer <p>…</p> of every parameter description,
  leaving a stray `</p><p>` inside a <span> for multi-paragraph descriptions.

The page HTML should also survive post-build edits such as translation, so:

- Heading.svelte and the Docstring returns/group titles render prop text with
  {@html} (escaped): a plain {text} node is overwritten with the prop value
  during hydration.
- HfOption.svelte renders every option and hides the inactive ones, so the
  content of all options is in the HTML.
- docstring.js drops the `<script context="module">` block mdsvex prepends to
  every docstring fragment, which ended up as stray inline scripts
  ("Unexpected token 'export'").

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@mishig25
mishig25 merged commit 90ddd19 into main Oct 13, 2026
5 checks passed
@mishig25
mishig25 deleted the fix/hydration-safe-doc-components branch October 13, 2026 12:25
mishig25 added a commit that referenced this pull request Oct 21, 2026
- Translate huggingface.co/docs/hub instead of Transformers for now. Its
  pages are already built by a doc-builder with #851, unversioned (`main`
  only), and link to each other relatively, so translated pages link to
  translated pages.
- Reject an empty answer instead of erasing the paragraph (review).
- A closing tag must close the innermost open tag of the same kind and
  number (review).
- Compare each tag's shape (kind, self-closing, parent): a link answered as a
  self-closing tag left its `<a>` unclosed and broke the page (review).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
mishig25 added a commit that referenced this pull request Oct 21, 2026
* Add translate-docs: incremental machine translation of built docs

`doc-builder translate-docs` translates the built English pages in
hf://buckets/hf-doc-build/doc (<library>/<version>/en/) with an LLM on HF
Inference Providers (Qwen/Qwen3.8-27B on Cerebras) and writes them to
<library>/<version>/<language>/ in a translation bucket, in the layout
moon-landing serves.

Only text changes, so a translated page hydrates against the same JS as the
English one (requires pages built after #851):
- blocks without Svelte hydration markers are sent as a light markup in which
  links and code may move to fit the target language; every tag must come
  back once, with the same nesting and unchanged code
- headings and docstring parameters only replace their text nodes, in order
- page links point to the translated folder, `_app` assets stay on English
- the page metadata, the sidebar (`_toctree.yml`) and copied Markdown are
  kept in sync with the pages

Like `populate-search-engine --incremental`, runs diff against a tracker:
`main` is re-checked daily and only changed pages are translated again,
deleted pages are removed, a release is translated once and then frozen, and
releases that predate a language are not backfilled. Translations are cached
per block by their exact prompt, so an edited page only sends its edited
paragraphs to the model. A block that fails validation stays in English and
is retried on the next run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Drop the translation glossary

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Translate Transformers into 11 languages, one job per language

Languages ranked by search reach: Simplified Chinese, Japanese, Korean,
Brazilian Portuguese, Spanish, French, Vietnamese, Indonesian, Turkish,
Russian and Arabic.

A first full pass takes hours per language, so the workflow now runs one
matrix job per library/language (generated from TRANSLATED_DOCS); each keeps
its own state and an interrupted language resumes on the next run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Address review: nested blocks, upload-then-record, run report, build trigger

- A block containing other blocks (e.g. a list item with a nested list) only
  replaces its own text nodes; its whole-range edit overlapped the nested
  block's edit and corrupted the HTML.
- Pages are recorded in the tracker only after their upload succeeded, so an
  interrupted upload is retried instead of being treated as done.
- Runs report what stayed in English: per-page counts in the tracker, a
  GitHub warning and job summary, and a failing run above 10% of blocks.
- build_main_documentation.yml can trigger translate_docs.yml right after
  the English docs are synced (opt-in `translation_dispatch_token` secret);
  the daily schedule stays as a fallback.
- `from huggingface_hub import BucketFolder`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Fix translation quirks found testing 8 languages on real Transformers pages

Tested pt, es, fr, vi, id, tr, ru and ar on five real pages built with the
current kit. The first pass kept 2.8% of blocks in English; the fixed run
keeps 1 of 3,288.

- Docstring JSON answers use a response schema with exactly the expected
  ids (models returned lists or split segments).
- Drop tags the source doesn't have (`<cN>ByteFallback</cN>`, a whole answer
  wrapped in `<comment>`); source text is escaped, so they were invented.
- "Repeated" checks count occurrences: code or fixed text that also appears in
  the English prose (e.g. `-100`) may appear in the translation.
- A code tag may appear twice with the same content (a restructured sentence
  can mention it again); links and formatting still exactly once.
- Reject answers that are still English, and name the target language next to
  the text: instruction-like sentences ("Use X to ...") were echoed reworded.
- The retry samples at temperature 0.5 instead of repeating the same answer.
- Don't double the kept punctuation when the model repeats it (", , ").
- Docstring parameter descriptions and headings are {@html} content, which
  hydration adopts as is: translate them in the reorderable markup mode, with
  the parameter name frozen. Verb-final languages (tr, ja, ko) now read
  naturally instead of following English word order.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Translate each page's Markdown next to the page

The built `<page>.md` (served by moon-landing for "Copy page" and
`Accept: text/plain`) was copied in English. It is now translated with the
same model prompt, validation and cache as the HTML:

- the page is split into units: paragraphs, list items, quote paragraphs,
  table cells and heading text; fenced code (also inside quotes and list
  items), heading anchors, list markers, quote prefixes, admonition markers
  and table pipes are kept as is
- inside a unit, inline code, images, autolinks, inline HTML and URLs are
  frozen; link text and emphasis are movable tags; a docstring parameter's
  name is not prose
- page links move to the translated folder, links to other libraries stay
- a unit that fails validation stays English and is retried, like a block

Tested on five real Transformers pages in ja, fr, tr and ar: every fenced code
line, URL, heading and anchor matches the English file.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Use the search tracker's token for buckets, a required token for inference

The buckets are read and written with HF_EMBED_DATASETS_TOKEN, the token the
search engine population already uses. Model calls use their own required
TRANSLATION_INFERENCE_TOKEN (`--inference_token`), so inference spending has
its own token.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Bill translation inference to the huggingface organization

Requests carry `X-HF-Bill-To: huggingface` (InferenceClient's `bill_to`), so
usage is billed to the organization instead of the token's owner. `--bill_to`
changes it; `--bill_to ''` bills the token's owner, e.g. for local runs.
https://huggingface.co/docs/inference-providers/pricing#billing-for-team-and-enterprise-organizations

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Translate the Hub docs; stricter checks from review

- Translate huggingface.co/docs/hub instead of Transformers for now. Its
  pages are already built by a doc-builder with #851, unversioned (`main`
  only), and link to each other relatively, so translated pages link to
  translated pages.
- Reject an empty answer instead of erasing the paragraph (review).
- A closing tag must close the innermost open tag of the same kind and
  number (review).
- Compare each tag's shape (kind, self-closing, parent): a link answered as a
  self-closing tag left its `<a>` unclosed and broke the page (review).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
mishig25 added a commit to huggingface/hub-docs that referenced this pull request Oct 21, 2026
Pins every doc-builder reusable workflow to huggingface/doc-builder@3c8f452
(main). The Hub, SageMaker and Xet docs move from 90ddd19; the API inference
docs from 9fc8a41, which also gives them the hydration fixes of
huggingface/doc-builder#851. The only interface change is a new optional
`translation_dispatch_token` secret.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant