Add a WYSIWYG editor to Next.js
Use the React component in the App Router: a client component for the editor, a server action to save and a route handler for uploads. No API key, MIT licensed.
Step 1: Install the packages
The React component works with React 17, 18 and 19. It creates the editor in an effect, so server rendering outputs an empty element and the page hydrates without warnings.
npm install wds-editor wds-editor-react
npm install sanitize-html && npm install -D @types/sanitize-html
Step 2: Create a client component
Keep the HTML in state and copy it to a hidden input, so a plain <form> with a server action receives it. The stylesheet can be imported here; Next.js allows global CSS from packages in any component.
'use client';
import { useState } from 'react';
import { WdsEditor } from 'wds-editor-react';
import 'wds-editor/style.css';
export function PostEditor({ name, initial = '' }: { name: string; initial?: string }) {
const [html, setHtml] = useState(initial);
return (
<>
<WdsEditor
value={html}
onChange={setHtml}
options={{
preset: 'standard',
height: 480,
fileManager: { listUrl: '/api/files', uploadUrl: '/api/files' }
}}
/>
<input type="hidden" name={name} value={html} />
</>
);
}
Step 3: Save with a server action
The page stays a server component. Clean the HTML in the action before it reaches the database; the editor is safe, but a request can be sent without it.
// app/posts/new/page.tsx
import { PostEditor } from '@/components/PostEditor';
import { savePost } from './actions';
export default function NewPostPage() {
return (
<form action={savePost}>
<PostEditor name="body" />
<button type="submit">Save</button>
</form>
);
}
// app/posts/new/actions.ts
'use server';
import sanitizeHtml from 'sanitize-html';
export async function savePost(formData: FormData) {
const body = sanitizeHtml(String(formData.get('body') ?? ''), {
allowedTags: sanitizeHtml.defaults.allowedTags.concat(['img']),
allowedAttributes: {
...sanitizeHtml.defaults.allowedAttributes,
'*': ['class', 'style', 'data-*'], // data-* keeps checklists and @mentions
img: ['src', 'alt', 'width', 'height']
}
});
// await db.post.create({ data: { body } });
}
Step 4: Add the upload route
A route handler lists and receives files. This version writes to public/uploads, which is fine on your own server in development. Files added to public after the build are not served by next start, so in production save to object storage (S3, R2, Vercel Blob) and return its URL.
import { NextResponse } from 'next/server';
import { mkdir, readdir, stat, writeFile } from 'node:fs/promises';
import path from 'node:path';
import { randomUUID } from 'node:crypto';
const dir = path.join(process.cwd(), 'public', 'uploads');
const allowed = new Set(['.jpg', '.jpeg', '.png', '.gif', '.webp', '.pdf']);
export async function GET() {
await mkdir(dir, { recursive: true });
const names = (await readdir(dir)).filter((n) => allowed.has(path.extname(n).toLowerCase()));
const files = await Promise.all(
names.map(async (name) => ({ url: `/uploads/${name}`, name, size: (await stat(path.join(dir, name))).size }))
);
return NextResponse.json({ files });
}
export async function POST(request: Request) {
// Check the session here (Auth.js, Clerk…) before accepting files
const file = (await request.formData()).get('file');
if (!(file instanceof File)) return NextResponse.json({ error: 'No file' }, { status: 400 });
const ext = path.extname(file.name).toLowerCase();
if (!allowed.has(ext) || file.size > 10 * 1024 * 1024) {
return NextResponse.json({ error: 'Images or PDF files up to 10 MB.' }, { status: 400 });
}
const name = `${randomUUID()}${ext}`;
await mkdir(dir, { recursive: true });
await writeFile(path.join(dir, name), Buffer.from(await file.arrayBuffer()));
return NextResponse.json({ url: `/uploads/${name}`, name });
}
Good to know
- Pages Router: the same component works; import it normally, no
dynamic()withssr: falseis needed. - Storage without a route: the
fileManager.uploadoption takes a function, so you can upload straight to a signed S3 URL and return the file's URL. - Show saved HTML with
dangerouslySetInnerHTML={{ __html: post.body }}only after it was cleaned on the server. - Every option, the toolbar items and the file manager JSON format are in the documentation; the server examples add folders, rename, delete and an AI endpoint.
FAQ
Does WDS Editor work with server-side rendering in Next.js?
Yes. The React component creates the editor in an effect, so the server renders an empty element and the browser adds the editor after hydration. Mark the file that uses it with 'use client' in the App Router.
Can I use WDS Editor on Vercel?
Yes. The editor itself is static JavaScript. For uploads, store files in Vercel Blob, S3 or R2 instead of the public folder, because files written after the build are not served.
Is there TypeScript support?
Yes. wds-editor and wds-editor-react ship their own types, including every editor option, so the options prop is checked and autocompleted.
Guides for other frameworks
Try WDS Editor in your browser
Free and MIT licensed, with a file manager, tables, Markdown and code blocks built in. No key, no account, no load limits.