Server Rendering
Parsing and preparation do not depend on React, so they can run on the server, at build time, or in a worker. The client only loads react-wikitext/view and restores a render snapshot.
Two deployment shapes
The second shape keeps parsing cost on the server; the client no longer loads wikiparser-node.
Prepare on the server
restoreWikitextRenderDocument revalidates schema versions, the root, HAST structure, and the node budget, then reruns sanitization and finalization. It does not trust arbitrary third-party HAST; only restore snapshots from a trusted server or cache.
A snapshot contains the semantic tree, HAST, and source, so it is noticeably larger than plain HTML. Prepare large documents per page or per block, and budget the transfer.
Next.js App Router
The root entry and /view create Context at module load and use React hooks, so they only run in Client Components. Both entries ship a "use client" directive, so the host does not need to create the boundary; the server only uses /parse and /hast.
- The server preparation step only calls
/parseand/hast; neither entry loads React. - Client Components restore and render; they never parse again.
- The root entry and
/viewship"use client", so Next.js treats them as client boundaries: they can be rendered from a Server Component, but parsing then happens on the client. When parsing must stay on the server, prepare the snapshot in the page component shown above. - In other SSR setups, render
WikitextViewwithrenderToStaticMarkupand hydrate from the same snapshot on the client.
Vite and other SSR setups
The idea is the same: prepare the snapshot on the server, send it as page data, and restore it on the client with /view.
- Escape
<when inlining JSON so</script>cannot terminate the script early. - If the page must expose content to crawlers, render
WikitextViewon the server too; the client must hydrate the same snapshot and tree.
Hydration and anchors
headingIdStrategy: "instance"uses auseIdprefix; it matches when the server and client render the same tree.- If the trees differ, or a page has multiple instances, pass an explicit
idPrefix. - Use
headingIdStrategy: "mediawiki"when the host already links to bare anchors such as#Section; it assumes a single instance per page. headingIdStrategy: "none"writes no ids and generates no TOC links, which suits embedded content without anchors.
Caching
Render snapshots are cacheable, but the key must cover every input that affects output:
AST_SCHEMA_VERSION,RENDER_SCHEMA_VERSION, and the parser version.- Source and
pageName. - Site config (
config). prepareOptions: resolvers, plugins,policy,labels,headingIdStrategy, andhandlers.
Restore rejects snapshots with a mismatched version; invalidate the cache when upgrading the library or parser. Parse caching is covered in Performance and budgets.
Related
- Basic usage: source component and explicit pipeline.
- Preparation
/hast: serialization, restore, and budgets. - Security model: the snapshot trust boundary.