May 10, 2023
3 min read
Quill + Next.js Failed to collect page data … Document Undefined
#react
#next.js
#quill
#editor
Hi There! 👋
If you are using Quill Editor with Next.js, Initialising Quill Editor is a bit tricky.
Sometimes in the dev environment, you might not see any issues, but when building your app there might be an issue called document undefined
Which will block you from deploying your app to the production
Before knowing the solution, Let’s understand why we are getting this error.
Quill.js Runs in the browser environment and because of that, it relies on the browser’s document object. (because of this you are getting document undefined when building your app)
Possible Solutions,
-
Make sure you use dynamic imports to only load and render Quill.js on the client side.
const ReactQuill = dynamic(import('react-quill'), { ssr: false, loading: () => <p>Loading ...</p>, })ssr: falseto render Quill Editor only on client sideloadingto show a template component or html when loading Quill Editor
-
Quill.js allow you to overwrite and add modules to editor, in my case I was trying to add a list of font sizes. And this is how you need to overwrite font size module
const fontSizeArr = ['8','9','10','12'] const Size = Quill.import('attributors/style/size'); Size.whitelist = fontSizeArr; Quill.register(Size, true);fontSizeArrlist of font size to overwrite existing font sizesQuill.importis to import module from Quill's internal modules.Size.whitelistallows the users to select and use the new font sizesQuill.registertakes two argumentsSizenew module to be overwritten on Quill Editortruetells Quill to overwrite the existing "Size" module with the customized version that you just configured
You might think why i am explaining Quill Modules to fix the error Document Undefined , because most of the time you face this error when overwriting or adding module to Quill.js
And to resolve this error,
As I said earlier, Quill relies on the browser’s
documentobject.In React, only after mounting the component you can use
documentorwindowobject.So, It is always best to initialise
Quilland itsmoduleswhen thecomponent mountsAnd this can be done by initializing Quill and its modules inside useEffect
useEffect(() => { if (typeof window !== 'undefined') { const Quill = require('react-quill').Quill const Size = Quill.import('attributors/style/size') Size.whitelist = fontSizeArr Quill.register(Size, true) } }, [])By initializing
Quilland itsmoduleon component mounting, you can resolve this issue.
Hope this post resolves your issue.
If you still face this issue after following all the best practice I have mentioned when using Quill.js
DM me on twitter, Let’s try to figure it out.