DEV Community

Samuel Bartík
Samuel Bartík

Posted on

Next.js: Custom server and Automatic Static Optimization

Hello everyone!

I am having trouble understanding why in the docs it is stated that having a custom server disables Automatic Static Optimization.

Before deciding to use a custom server, please keep in mind that it should only be used when the integrated router of Next.js can't meet your app requirements. A custom server will remove important performance optimizations, like serverless functions and Automatic Static Optimization.

My understanding is that thanks to it, during the build phase (next build) it will automatically generate an HTML file (for pages that qualify) which will be then served in future requests.

Now, what is preventing nextjs handler in the custom server from using already generated files from the next build step and serving them just as the built-in server would?

I tested serving app with the built-in server (next start) and with a custom server and I have not found any difference between them, even when it comes to the Automatic Static Optimization. In both cases, static HTML files were generated for a simple page with no getServerSideProps or getInitialProps and it served the page without a problem.

Have I misunderstood what the Automatic Static Optimization is really doing? Or something else?

Thanks!

Discussion (1)

Collapse
maxfindel profile image
Max F. Findel

From what I understand after reading the docs, what you miss out by using a custom server is the "optimization" part, not the "serving static HTML" part.

What I mean by this, is that if you use a custom server the request will get to your app and if there are no getServerSideProps nor getInitialProps in your page, it will serve the pre-built HTML.

But if you're using the default Next.js server the request doesn't even need to get to your app. Instead, the server will serve the HTML cached in the nearest CDN.
Does this make sense?