Exporting Static HTML From A Nextjs 10 Site
In this blog post, we're going to take the default start from NextJS 10 and export a static site that we can host in S3.
This post goes alongside a partner post on deploying static websites to S3 using the AWS TypeScript CDK if you would like to get the site up and running on your AWS account.
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.
We will use the latest
create-next-app deployment to get things rolling.
Once this runs, there will be a new folder that hosts your NextJS 10 application in
Building the application
Adjust your application's
package.json file to add a new script for export:
Building and exporting the website
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:
Once you have run both commands, you'll notice that there is now a
out directory created.
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.
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.
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.
Image credit: Isi Parente
See how you can dynamically create UIs based on React State using dynamic imports, Next.js 10 and React State
Have you ever wanted to build a UI Component Library with TypeScript and React? This blog post will take you through a straightforward set up that uses the bare minimum to get a working component library that you can re-use across your different React projects.
This blog post will show you how I updated my Gatsby website to display a basic related articles section based on frontmatter tags.
This post will go through a simple example of setting up simple authentication with a user and password to your Next.js website.
1,200+ PEOPLE ALREADY JOINED ❤️️
Get fresh posts + news direct to your inbox.
No spam. We only send you relevant content.