|
| 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 |
0 commit comments