This is a live Next.js project bootstrapped with create-next-app.
This is the first solution I found to get a next.js project deployed on Github Pages.
First, run the development server:
npm run dev
# or
yarn dev
# or
pnpm dev
# or
bun devOpen http://localhost:3000 with your browser to see the result.
You can start editing the page by modifying app/page.tsx. The page auto-updates as you edit the file.
This project uses next/font to automatically optimize and load Inter, a custom Google Font.
To learn more about Next.js, take a look at the following resources:
- Next.js Documentation - learn about Next.js features and API.
- Learn Next.js - an interactive Next.js tutorial.
You can check out the Next.js GitHub repository - your feedback and contributions are welcome!
This comes from a solution I found through gregrickaby's github page Below is the documentation from his repo for reference.
Deploy Next.js to GitHub Pages with GitHub Actions. View the deployed app 🚀
Now with Next.js App Router support! If you need Pages Router support click here.
⚠️ Heads up! GitHub Pages does not support serverless or edge functions. This means dynamic functionality will be disabled. See all the unsupported features.
First, you need to configure Next.js to deploy static exports. This is required for GitHub Pages to work.
- Open the
next.config.mjsfile - Add the following:
/** @type {import('next').NextConfig} */
const nextConfig = {
/**
* Enable static exports for the App Router.
*
* @see https://nextjs.org/docs/app/building-your-application/deploying/static-exports
*/
output: "export",
/**
* Set base path. This is the slug of your GitHub repository.
*
* @see https://nextjs.org/docs/app/api-reference/next-config-js/basePath
*/
basePath: "/next_ghpages",
/**
* Disable server-based image optimization. Next.js does not support
* dynamic features with static exports.
*
* @see https://nextjs.org/docs/app/api-reference/components/image#unoptimized
*/
images: {
unoptimized: true,
},
};
export default nextConfig;-
Save the
next.config.mjs -
Finally, place a
.nojekyllfile in the/publicdirectory to disable GitHub Pages from trying to create a Jekyll website.
.
├── app/
├── public/
│ └── .nojekyll
├── next.config.js
Perfect! This is all you need to configure Next.js to deploy on GitHub Pages.
Next, you will need to add the base path to images in page.tsx file. This is required for the images to appear on GitHub Pages.
- Open
app/page.tsx - Find the
Imagecomponents - Add
/nextjs-github-pages/(or the slug of your GitHub repository) to thesrcprop:
<Image
src="/nextjs-github-pages/vercel.svg"
alt="Vercel Logo"
className={styles.vercelLogo}
width={100}
height={24}
priority
/>
- Save the
page.tsxfile
Learn more by reading the official documentation for basePath and images.
Next you need to configure Github for automated deployments via GitHub Actions.
The following settings use the Github Action Deploy Pages to deploy. I prefer this workflow because you don't need to generate SSH keys or use a personal access token.
- Go to your repository's Settings tab
- Click "Pages" in the sidebar
- Under "Build and Deployment", select "GitHub Actions" as the source:
This is where the magic happens! This workflow file will automatically build and deploy the app when you push to the main branch.
- Create
.github/workflows/deploy.ymlfile - Paste the contents of https://github.com/gregrickaby/nextjs-github-pages/blob/main/.github/workflows/deploy.yml
- Save the
deploy.ymlfile
Now that everything is configured, you can commit your code and push to GitHub. This will trigger the GitHub Action workflow and deploy your app to GitHub Pages.
git add . && git commit -m "Initial commit" && git pushYou should see your site deployed to GitHub Pages in a few minutes. 🚀
