Skip to content

Commit e419b9f

Browse files
Added Invalid Schema Handling
1 parent 5bf788e commit e419b9f

File tree

4 files changed

+128
-0
lines changed

4 files changed

+128
-0
lines changed

Diff for: .idea/.name

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: src/content/guides/index.mdx

+15
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,21 @@ Tiptap Guides provide practical advice on configuring your editor, enhancing the
6161
</Link>
6262
</CardGrid.Item>
6363
</FilterGrid.Item>
64+
<FilterGrid.Item filter="Editor">
65+
<CardGrid.Item asChild>
66+
<Link href="/guides/invalid-schema">
67+
<CardGrid.Subtitle size="sm">Essential</CardGrid.Subtitle>
68+
<div>
69+
<CardGrid.ItemTitle>
70+
How to handle invalid schemas in Tiptap
71+
</CardGrid.ItemTitle>
72+
</div>
73+
<CardGrid.ItemFooter>
74+
<Tag>Editor</Tag>
75+
</CardGrid.ItemFooter>
76+
</Link>
77+
</CardGrid.Item>
78+
</FilterGrid.Item>
6479
<FilterGrid.Item filter="Editor">
6580
<CardGrid.Item asChild>
6681
<Link href="/guides/output-json-html">

Diff for: src/content/guides/invalid-schema.mdx

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
---
2+
tags:
3+
- type: editor
4+
meta:
5+
title: Invalid schema handling | Tiptap Editor Docs
6+
description: Adjust and configure how you handle invalid schemas in Tiptap and Tiptap Collaboration. More in the docs!
7+
category: editor
8+
title: Invalid schema handling in Tiptap
9+
---
10+
11+
Content integrity can be a significant challenge in collaborative editing environments. Think of a scenario where users with different versions of an app are trying to edit the same document. Or a scenario common to single-page applications: some users keep their browser tabs open for long periods of time without refreshing, while others always have the latest editor version after a recent page load.
12+
13+
In both cases, a user with a newer version is creating content with features that are not available to others. When a user with an older version tries to access this document, how do we prevent data loss, maintain document structure, and ensure a smooth user experience?
14+
15+
This is where invalid schema handling becomes important. It allows developers to gracefully manage situations where the content structure doesn't match the editor's expectations, preventing issues like data corruption, unexpected content stripping, or editor crashes.
16+
17+
Whether you're building a note-taking app, a content management system, or any application with rich text editing capabilities, understanding and implementing proper schema handling can significantly improve your application's reliability and user experience.
18+
19+
## Introducing content checking
20+
21+
Tiptap has a option called `enableContentCheck`. When set to `true`, this feature activates a mechanism to validate content against the schema derived from your registered extensions. This is particularly useful for catching and responding to content errors before they cause issues in your application.
22+
23+
### Enable content checking
24+
25+
To enable this feature, simply add the `enableContentCheck` option when initializing your Tiptap editor:
26+
27+
```jsx
28+
new Editor({
29+
enableContentCheck: true,
30+
// ... other options
31+
})
32+
```
33+
34+
## The contentError event
35+
36+
When content checking is enabled, Tiptap will emit a `contentError` event if the initial content provided during editor setup is incompatible with the schema. This event provides you with valuable information to handle the error appropriately.
37+
38+
### Handle contentError events
39+
40+
You have two options for handling these events:
41+
42+
1. Using the `onContentError` callback:
43+
44+
```jsx
45+
new Editor({
46+
enableContentCheck: true,
47+
content: potentiallyInvalidContent,
48+
onContentError({ editor, error, disableCollaboration }) {
49+
// Your error handling logic here
50+
},
51+
// ... other options
52+
})
53+
54+
```
55+
56+
1. Attaching a listener to the `contentError` event:
57+
58+
```jsx
59+
const editor = new Editor({
60+
enableContentCheck: true,
61+
content: invalidContent,
62+
// ... other options
63+
})
64+
65+
editor.on('contentError', ({ editor, error, disableCollaboration }) => {
66+
// Your error handling logic here
67+
})
68+
69+
```
70+
71+
## A note on content types
72+
73+
It's important to note that Tiptap's content checking is 100% accurate for JSON content types. However, when working with HTML content, there may be some limitations. While Tiptap does its best to alert on missing nodes, certain mark-related issues might be missed in some situations.
74+
75+
## Recommended error handling strategies
76+
77+
How you handle schema errors will depend on your specific application and requirements. However, here are some general recommendations:
78+
79+
### For non-collaborative editing
80+
81+
If you're not using collaborative editing features, the default behavior of stripping unknown content may be sufficient. This keeps your content in a known valid state for future editing.
82+
83+
### For collaborative editing
84+
85+
When using collaborative features, it's crucial to handle content errors to prevent synchronization issues. Here's an example of how you might handle a content error in a collaborative environment:
86+
87+
```jsx
88+
onContentError({ editor, error, disableCollaboration }) {
89+
// Disable collaboration to prevent syncing invalid content
90+
disableCollaboration()
91+
92+
// Prevent emitting updates
93+
const emitUpdate = false
94+
95+
// Disable further user input
96+
editor.setEditable(false, emitUpdate)
97+
98+
// Notify the user about the issue
99+
notifyUser("An error occurred. Please refresh the application.")
100+
}
101+
102+
```
103+
104+
This approach:
105+
1. Disables collaboration to prevent syncing invalid content
106+
2. Prevents updates from being emitted
107+
3. Disables the editor to prevent further user input
108+
4. Notifies the user about the issue

Diff for: src/content/guides/sidebar.ts

+4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ export const sidebarConfig: SidebarConfig = {
2929
href: '/guides/performance',
3030
title: 'Performance',
3131
},
32+
{
33+
href: '/guides/invalid-schema',
34+
title: 'Invalid schema handling',
35+
},
3236
{
3337
href: '/guides/output-json-html',
3438
title: 'Export JSON or HTML',

0 commit comments

Comments
 (0)