Home > Web Front-end > JS Tutorial > Make EditorJS work in Svelte(kit) SSR

Make EditorJS work in Svelte(kit) SSR

Linda Hamilton
Release: 2025-01-02 19:14:39
Original
277 people have browsed it

Make EditorJS work in Svelte(kit) SSR

Here's a refined version of your post:


If you're here, you've probably been having issues using EditorJs in Sveltekit (like me). Since SSR isn't supported in EditorJs (see discussion), you might encounter errors like this:

[vite] Error when evaluating SSR module /src/routes/+page.svelte: failed to import "@editorjs/editorjs"
|- ReferenceError: Element is not defined
Copy after login

Here's how I solved it:

  1. Load Editor Asynchronously: Ensure the editor loads only on the client side using onMount to avoid SSR complications.

  2. Element Initialization: Bind elements properly and handle initialization using onMount to ensure the element is available after component setup.

  3. Be Sure To Import EditorJs Correctly (since it's a default export):

    • Default Import:
     const { default: EditorJs } = ...
    
    Copy after login
  • Destructuring Import:

     const Editor = ...
    const EditorJs = Editor.default
    
    Copy after login

Here's the full solution:

Happy Hacking! <script></script>

The above is the detailed content of Make EditorJS work in Svelte(kit) SSR. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template