Build a Next.js Website With Custom Domain in 10 Minutes
In this tutorial, we’re going to use
create-next-app, Github, Netlify, and namescheap to deploy a Next.js static website with a custom domain name.
Next.js is a “production ready” library that uses React, with added benefits such as server-side rendering and routing out of the box. For more information, check out nextjs.org.
There are certainly other options for domain name registration and site hosting, but Namescheap provides the least expensive domains I’ve found, is easy to work with, and also comes with whoIs privacy protection for free (so you won’t be spammed with calls and emails when you register a new domain). Vercel is a very easy hosting platform.
Note: this method applies only to static sites generated with Next.js. Dynamic sites will require a node.js hosted backend.
STEP 1 OF 5: Create your app
To start, create a new Next.js application called ‘my-awesome-app’ using
create-next-app: (you can of course change the name of the application to be whatever you want)
In the terminal, type:
npx create-next-app my-awesome-app
IMPORTANT: In the
package.json file, change the
"build" command to
"next build && next export"
STEP 2 OF 5: Push your app to GitHub
Next, create a new GitHub repo for the project, and then upload your project to it.
In GitHub, click on the “New” button in the top left:
Type the name of the app, and click the “create repository” button at the bottom of the screen:
Copy the code to push your existing repository from the command line:
Paste the code into the terminal to upload your code.
STEP 3 OF 5: Set up Netlify for hosting
Next, log into Netlify (create an account if you haven’t already).
Click on “New site from Git”
On the next screen, select “GitHub”
On the next screen, scroll down to find your GitHub repo for the app:
Then, on the next screen click “Deploy site”
For the “Build command” type
yarn build, and for the “Publish directory” type
Next, click on “Domain settings”
Next, click on “Add custom domain”
Type in your domain and click verify
Then click “Yes, add domain”
Back on the Domain Settings screen, copy the .app link. You’ll use this later after you register the custom domain.
STEP 4 OF 5: Register your custom domain name at Namescheap
Register a domain name on Namescheap
After your purchase is complete, go into your Dashboard, and find the domain name you want to transfer, and click the “MANAGE” button
Next, click on “Advanced DNS”
Click on “Add New Record”
Scroll down to CNAME and click
For the “Host” type “www”, and for the “Value” paste your .app link from Netlify. Hit the green check mark and you are good to go.
STEP 5 OF 5: Add your domain to Netlify
Your custom domain name should now show up on Netlify:
Head on over to your new site and start customizing it :). When you push to your GitHub repo your site will automatically update.
Thanks for reading.
If you created a webapp using this method, I’d love to know! Please leave a comment below.
Note: the namescheap link is an affiliate link. I use namescheap for all of my personal websites as I’ve found them to have the best and cheapest services. Feel free to go directly to their website instead.