Deploying a static Nextjs app on Github Pages for free
Deploying a static Nextjs app on Github Pages for free
The problem
We are refreshing our axlessoft.com website.
It was Wordpress before and but it was a bit broken because it was not updated in a while.
We decided to do a static Nextjs website:
- Static because there is nothing dynamic, it is a portfolio website afterall
- Modern. We can use Tailwind to create a nice design
- It’s free to host
Setup
“Hey, [insert LLM name], build a nextjs portfolio website. Make no mistakes”
Hosting on Github pages
- We go to our repo
- Go to
Settings -> Pages -> "Build and deployment" -> Sourceand change it to GitHub Actions
Now for the “coding” part: I use pnpm, so we need to set it up a bit differnetly than this very useful post: https://github.com/orgs/community/discussions/191018#discussioncomment-16366710
Create .github/workflows/deploy.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
name: Deploy Next.js site to Pages
on:
push:
branches: ["main"] # Ensure this matches your default branch name
workflow_dispatch: # Allows you to run this workflow manually from the Actions tab
# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
permissions:
contents: read
pages: write
id-token: write
# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued.
concurrency:
group: "pages"
cancel-in-progress: false
jobs:
# Build job
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup pnpm
uses: pnpm/action-setup@v4
with:
version: 10
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: "20"
cache: "pnpm"
- name: Install dependencies
run: pnpm install --frozen-lockfile
- name: Build with Next.js
env:
BASE_PATH: /axlessoft_website
run: pnpm run build
- name: Upload artifact
uses: actions/upload-pages-artifact@v3
with:
path: ./out # Next.js exports static files to the 'out' directory by default
# Deployment job
deploy:
environment:
name: github-pages
url: $
runs-on: ubuntu-latest
needs: build
steps:
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4
And add output: "export", to next.config.ts
1
2
3
4
5
6
7
8
import type { NextConfig } from "next";
const nextConfig: NextConfig = {
output: "export",
/* config options here */
};
export default nextConfig;
Adding a custom domain
We want to use our own domain:
- Verifying the domain
- Configuring the domain
- Add it to the repo: Settings -> Pages -> Custom Domains
It could give an error “NotServedByPagesError”, but you should just wait. It takes time for DNS records to propagate
This post is licensed under CC BY-NC 4.0 by the author.