In Next.js, Why static render? _document.js includes getInitialProps

625 views Asked by At

I'm very confused about the _document.js file in Next.js and wondering why it seems to violate the rule in Next.js that says "getInitialProps will disable automatic static optimization".

Clearly, it does not as you can see when you run a production build. The resulting output claims that pages will be statically optimized. Since ever page runs through _document.js, none of them should be able to be statically optimized.

import Document, { Html, Head, Main, NextScript } from "next/document";

class MyDocument extends Document {
  static async getInitialProps(ctx) {
  const initialProps = await Document.getInitialProps(ctx);
  return { ...initialProps };
   }

   render() {
1

There are 1 answers

0
leerob On BEST ANSWER

getInitialProps will disable automatic static optimization if you make an async call to fetch data. In the example above, it will not prevent creating static pages.