In an earlier adventure, we created a statically generated Next.js blog using Next.js 14, Tailwind Typography and MDX.
If you want more information, you may view that post here.
This time, we are exploring the option to host our static blog site on Github Pages.
Github Pages allows free users to host their static websites if the repository is public which is a great option for our endeavour.
There are some usage limitations for github-pages that you might want to read here before we continue.
Github Repo
If you want to visit the final product and explore the code yourself or just clone the project and make your own blog; visit the github repo here.
You may also visit the blog site deployed to github-pages in its final form here.
First steps
Weather you are using the mdx-blog project or your own next.js project; there are a few things we need to make sure before continuing.
Make sure next is configured to export static site. Go to your project root and open your next.config.js or next.config.mjs
Verify build
We should run next build command to verify that nothing fails and we get our static site at /out folder. When we run a build, next will show the route structure in a tree. All routes must be static since we are pre building each possible route.
Here is what it should look like after a build in the terminal
After we make sure our build doesn't fail, all routes are static and our next config is set to output: "export", we may continue.
Configuring the Github Repo
Now that our project is ready for publishing, we commit our changes and upload it to our github repo. Lets now make sure in the repository settings that the visibility is set to public if your account is also a free tier.
Go to settings tab in your github repo and click on pages from the side menu
Select Github Actions
We may now choose a deployment template. A next.js template is suggested already so we choose it or we can just click browse and find the next.js template ourselves.
click customize and view the yaml file that'll deploy our project.
Now you may click on commit changes and try your luck but it didn't work for me because at the time I was writing this blog, the suggested workflow was configured for an older next.js version.
If the build fails
We can go to our code editor now and fetch the new workflow to our project from the github repo.
.github/workflows/nextjs.yml file appears in my project. Lets fix this now
This part is the first culprit. It's writing over our own next.config. We need to remove this step.
The second culprit is this part that belongs to an older version. We only use the next build command now. So this part must be removed:
Final workflow
Here is what my final workflow looks like. Lets save it and see if the build works.
Perfect it worked and we can go to https://<username>.github.io/<repository-name>/ to view the blog.
But we have a problem. Because we removed the basePath step from the workflow, all url's in our application are broken including links to css files. We can only see the raw text as you can see below.
Fixing our basePath
If you are hosting your app at the root level of a url like yourblog.com/ there wont be any issues. You can connect a custom domain to your github pages and it'll work. But if you want to be able to host your blog using your github url like https://<username>.github.io/<repository-name>/ we must tell our application that the basePath is /<repository-name>. This will fix all the relative url's.
Et voila! all relative url's are now working
How it all works
Thanks to github workflows, we've just implemented our continuous integration and continuous deployment (CI/CD) pipeline.
When we make a change like add a new mdx file blog post and commit our repo to github, github actions will start the workflow process.
It'll create a virtual machine, install all our dependencies, run the next build command and deploy the /out folder to github-pages.
Once we've set this up, everything is automated and we get all this for free. Thanks GitHub!