next.jsserver-side-rendering

How to remove __NEXT_DATA__ from dom in NextJs?


Because of __NEXT_DATA__ element DOM size is getting very high and it is affecting to the performance. Can anyone plz help me to remove __NEXT_DATA__ from DOM? I am using full server-side rendering with dynamic routes in Next.js.


Solution

  • TLDR: If you would(/could) remove the __NEXT_DATA__ script tag, React wouldn't be able to hydrate. You either hardcode the data in the page or try to reduce your pageProps payload, returned from getServerSideProps.

    I came upon this issue also recently, where I asked myself, why would there be a need for the content to be included in the HTML 2 times.

    1. As the content itself - when your NextJS renders the appropriate HTML and sends it to the client
    2. As JSON in <script> tag - This is because of the need for rehydration on the client side.

    "Solutions"

    Reading

    1. Here's a github link with related discussion in next in which you might be interested.
    2. Blog post about reducing the size of __NEXT_DATA__ - by Liran Cohen.