1

I am using the Nextjs framework and my pages are dynamically generated.

When I view the page source code - it is displayed in an unreadable format.

Is it possible to create a 'pretty' formatted HTML for the application created with NextJs?

I have such request from an SEO optimization manager.

View page source modal

Page source

ksav
  • 20,015
  • 6
  • 46
  • 66
Devzhen
  • 51
  • 6
  • Does using the inspector achieve your goal? The inspector is the next option down in the menu photo. – Sean W Mar 09 '22 at 02:05
  • The main goal is to have a pretty formatted output HTML. It is a request of the SEO optimization manager. – Devzhen Mar 09 '22 at 08:11
  • https://swc.rs/docs/configuration/minification – ksav Mar 09 '22 at 08:44
  • I actually get why this was asked. When you use inspector, you get formatted HTML whether you are using Next or CRA - there is something about viewing source (and it being formatted) that strangely assures one that their content is truly server side rendered. Call it OCD or whatever but I get why the question was asked....even though as rightfully answered, its not necessary. – Andy May 06 '22 at 07:04
  • The inspector does not show you what HTML was delivered from the server; it shows you what the HTML DOM tree current is. These are very, very different things. In production, having the server return minified/compressed HTML is great for the end user. In development, it's sometimes a chore to wade through compressed HTML to find what you're looking for. Debugging an SSR bug in a large site can be quite painful, and the burden would be less if the HTML could be pretty-printed natively out of Next.js. – BU0 Aug 09 '22 at 19:46

1 Answers1

3

Received this answer from NextJs team

There is no "options" within Next.js to achieve this.

One way to do it would be to create a custom server and prettify the response yourself. While I don't advocate to make a custom server just for this, some might have found success in doing so.

I find it "weird" to prettify HTML for SEO reason though. Search engines are well capable of parsing minified HTML and "prettifying" the output will add weight to the page.

More height means less performance and that could impact SEO in a negative way.

Devzhen
  • 51
  • 6
  • They may find it "weird" but until we live in a world where we can rely on all tools to reliably and properly pretty print the output when inspecting a response it's still a nice quality of life feature. Alas, we don't live in such a world. – ChrisM Jul 25 '23 at 22:45