Building Blazing Fast Static Websites with Next.js: A Beginner's Guide


Introduction
Want to build a super fast website that loads instantly? You've probably heard about Next.js, a powerful framework for React. But did you know you can use Next.js to create websites that are entirely static? That means no server-side rendering (SSR), no client-side JavaScript (after the initial load, anyway!), just plain old HTML, CSS, and JavaScript ready to be served directly from a CDN. This tutorial will guide you through creating such a project, perfect for blogs, documentation sites, or any content that doesn't require dynamic data updates.
Prerequisites & Setup
Before we dive in, make sure you have the following installed:
Node.js (version 16 or later): This is the runtime environment for JavaScript. Download it from nodejs.org.
npm or Yarn (optional): These are package managers for JavaScript. npm comes bundled with Node.js. Yarn is an alternative.
A code editor: VS Code, Sublime Text, or any editor you prefer.
Once you have Node.js installed, open your terminal or command prompt and create a new Next.js project:
npx create-next-app my-static-site
cd my-static-site
This command uses create-next-app
, the official Next.js tool, to scaffold a new project for you. It will ask you a few questions about your project; you can generally accept the defaults.
Step-by-Step Implementation
Now, let's modify the project to generate static files only.
1. Understanding the File Structure
pages/
: This directory is crucial. Each.js
file insidepages
becomes a route in your application. For example,pages/index.js
corresponds to the root route (/
), andpages/about.js
corresponds to the/about
route.public/
: This directory holds static assets like images, fonts, and other files that you want to serve directly.
2. Creating a Simple Page
Open pages/index.js
and replace its content with the following:
function HomePage() {
return (
<div>
<h1>Welcome to my Static Site!</h1>
<p>This site is generated statically using Next.js.</p>
</div>
);
}
export default HomePage;
This is a basic React component that displays a heading and a paragraph. Notice that we're not fetching any data or performing any complex logic here.
3. Generating the Static Site
Next.js provides a command to export your application as static HTML files. Open your package.json
file (in the root of your project) and find the scripts
section. Add a new script called export
:
{
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint",
"export": "next export"
}
}
Now, run the following commands in your terminal:
npm run build
npm run export
The npm run build
command builds your Next.js application. The npm run export
command then generates the static HTML files. These files will be placed in a new directory called out/
(or dist/
, depending on your Next.js version and configuration) in the root of your project.
Technical Deep-Dive: next export
The next export
command analyzes your pages
directory and generates static HTML files for each route. It essentially "pre-renders" your React components at build time, creating static assets that can be served directly by a web server or a CDN. Because everything is pre-rendered, the browser receives fully formed HTML, leading to incredibly fast initial load times.
4. Serving the Static Site
Now that you have your static files, you need a way to serve them. You can use a simple HTTP server for testing. One popular option is serve
:
npm install -g serve
serve out # or serve dist, depending on where your files are
This command starts a local web server and serves the contents of the out/
directory (or dist/
). Open your browser and navigate to [http://localhost:5000
](http://localhost:5000`) (or the address shown in your terminal) to see your static site.
Testing/Validation
To validate that your site is truly static, you can disable JavaScript in your browser. If the site still loads and functions correctly (without any dynamic content or interactive elements), then you've successfully created a static site. You can also inspect the page source in your browser's developer tools to confirm that the HTML is fully rendered.
Troubleshooting Common Issues
next export
command fails: Make sure you have anext.config.js
file in your project root withtrailingSlash: true
if you are using a static hosting provider that requires trailing slashes on all URLs.Images not loading: Ensure your images are located in the
public/
directory and are referenced correctly in your components (e.g.,<img src="/my-image.jpg" alt="My Image" />
).Links not working: Use the
<Link>
component fromnext/link
for internal navigation. For example:import Link from 'next/link'; function HomePage() { return ( <div> <h1>Welcome to my Static Site!</h1> <Link href="/about"> <a>About Us</a> </Link> </div> ); } export default HomePage;
Next Steps
Deploy to a CDN: Services like Netlify, Vercel, and AWS S3 are excellent for hosting static websites. They offer features like global content delivery, automatic deployments, and custom domains.
Add more pages: Create more
.js
files in thepages/
directory to add more routes to your site.Incorporate Markdown: Use a library like
remark
ormarkdown-it
to convert Markdown files into HTML and dynamically generate pages from them. This is a great way to manage content for blogs or documentation sites.Add styling: Use CSS Modules, styled-components, or Tailwind CSS to style your website. Remember to optimize your CSS for production to minimize file sizes.
Static Site Generation with Data: Although this tutorial focuses on completely static sites, Next.js offers ways to fetch data at build time using
getStaticProps
. This allows you to incorporate data from external sources into your static site.
Conclusion
Building static websites with Next.js is a powerful way to create incredibly fast and efficient web experiences. By leveraging the framework's static export capabilities, you can build sites that are easy to deploy, scale, and maintain. This tutorial provides a solid foundation for building your own static sites. Experiment, explore, and have fun!
Subscribe to my newsletter
Read articles from Sylvester Das directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
