Note: There are reasons for and against exporting static HTML for a NextJS application. Those needs may differ from person to person. Please see NextJS documentation on Static HTML Export for the latest caveats when using their static export feature.
Getting Started
We will use the latest create-next-app deployment to get things rolling.
# from the project root directory
npx create-next-app next-10-static-export
Once this runs, there will be a new folder that hosts your NextJS 10 application in next-10-static-export.
Building the application
Adjust your application's package.json file to add a new script for export:
{
// reduced for brevity
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
// add this
"export": "next export"
}
// reduced for brevity
}
Building and exporting the website
Under pages/index.js I am going to make some small adjustments for the project and add a small paragraph that says This page has been updated to show how to build and export static html from a NextJS 10 project.
After making whatever adjustments you would like, run the following from your root directory:
# Build the NextJS application
npm run build
# Export the HTML to the `out` directory
npm run export
Once you have run both commands, you'll notice that there is now a _next and out directory created.
The out directory is where next export will move the assets to.
Seeing the final product
We can now serve those assets locally to see the build in action.
If you do not have it already, I recommend globally installing Vercel's Serve package package to enable you serve the out directory. This can be done with npm i -g serve.
Now, run serve out from the root directory and you will have a URL posted to the terminal to show you where the website is being served from (generally localhost port 5000 unless busy).
Head to the website and you will see the following:
NextJS 10 Exported Site
Congrats! You have your statically exported site.
What's next?
If you would like to see how to deploy your website to AWS S3 using the AWS TypeScript CDK, head to the partner post and continue on.