Skip to content

Server-side rendering ​

The core and the adapters run on the server without touching the DOM. The editor itself can only mount in the browser, so on the server <EditorContent> renders an empty element, unless you give it the HTML to show.

Pass html made with toHTML() from the same document. The page shows the content immediately, and the editor takes over that element when it mounts. Only the first value is used.

tsx
'use client'
import type { DocJSON } from '@writeon/core'
import { basic } from '@writeon/core/presets'
import { toHTML } from '@writeon/html'
import { EditorContent, useEditor } from '@writeon/react'

const extensions = basic()

export function PostEditor({ doc }: { doc: DocJSON }) {
  const editor = useEditor({
    extensions,
    content: doc,
    label: 'Post',
  })

  // Rendered on the server: the text is visible before JavaScript
  // runs. The editor takes over this element when it mounts.
  const html = toHTML(doc, { extensions })

  return <EditorContent editor={editor} html={html} />
}
vue
<script setup lang="ts">
import type { DocJSON } from '@writeon/core'
import { basic } from '@writeon/core/presets'
import { toHTML } from '@writeon/html'
import { EditorContent, useEditor } from '@writeon/vue'

const props = defineProps<{ doc: DocJSON }>()
const extensions = basic()

const editor = useEditor({
  extensions,
  content: props.doc,
  label: 'Post',
})

// Rendered on the server: the text is visible before JavaScript
// runs. The editor takes over this element when it mounts.
const html = toHTML(props.doc, { extensions })
</script>

<template>
  <EditorContent :editor="editor" :html="html" />
</template>

Things to know:

  • Create extensions once (outside the component) and share the same array.
  • useEditor() reads content and label once, on creation. Use editor.setContent() to replace the document later.
  • The default kit's <Toolbar> reserves its height before it mounts, so the page does not jump.