What would cause a static HTML to be created for a non-static page using Next.js? I'm unsure if it's causing any issues, but I noticed this was happening while troubleshooting an issue with this specific page. Is this expected?
What I mean is that we have one page, let's call it "page1", that does not have the getStaticPaths()
or getStaticProps()
functions.
This page does rely on server-side data, but does not make use of getInitialProps()
or getServerSideProps()
as they are deprecated.
When I cd
into the build directory I can see the .next/server/pages/page1.html
- is this expected? There is no page1.json
file. If not, what would cause this HTML file to be created?
Yes, that's expected. It's Next.js' Automatic Static Optimization at play.
If your page does not contain getServerSideProps
/getInitialProps
, then Next.js will automatically statically optimize the page and prerender it as static HTML.
From the docs:
If
getServerSideProps
orgetInitialProps
is present in a page, Next.js will switch to render the page on-demand, per-request (meaning Server-Side Rendering).If the above is not the case, Next.js will statically optimize your page automatically by prerendering the page to static HTML.
During prerendering, the router's query object will be empty since we do not have query information to provide during this phase. After hydration, Next.js will trigger an update to your application to provide the route parameters in the query object.
next build
will emit.html
files for statically optimized pages.