nextjs custom document

  1. Create a new file called _document.js in the pages directory of your Next.js project.

  2. Import the necessary dependencies at the top of the file. These dependencies include Document from the next/document module and Html, Head, and Main from the next/document module.

  3. Create a new class called MyDocument that extends the Document class.

  4. Inside the MyDocument class, define a static method called getInitialProps. This method takes a parameter called ctx and returns a promise that resolves to an object containing the html, head, and styles properties.

  5. In the getInitialProps method, call the Document.getInitialProps method with the ctx parameter and store the result in a variable called initialProps.

  6. Create a new variable called styles and assign it the value of initialProps.styles.

  7. Return an object containing the html, head, and styles properties, with their respective values set to initialProps.html, initialProps.head, and styles.

  8. Below the MyDocument class definition, export the MyDocument class as the default export of the file.

By following these steps, you will create a custom document file in Next.js that allows you to customize the HTML document structure and add any required global CSS styles.