|
| 1 | +# Next.js GitHub Pages |
| 2 | + |
| 3 | +Deploy Next.js to GitHub Pages with GitHub Actions. [View the deployed app](https://gregrickaby.github.io/nextjs-github-pages/) 🚀 |
| 4 | + |
| 5 | +Now with Next.js App Router support! If you need Pages Router support [click here](https://github.com/gregrickaby/nextjs-github-pages/releases/tag/pages_dir). |
| 6 | + |
| 7 | +> ⚠️ Heads up! GitHub Pages _does not_ support serverless or edge functions. This means dynamic functionality will be disabled. See all the [unsupported features](https://nextjs.org/docs/app/building-your-application/deploying/static-exports#unsupported-features). |
| 8 | +
|
| 9 | +--- |
| 10 | + |
| 11 | +## Configure Next.js |
| 12 | + |
| 13 | +### Next.js Config |
| 14 | + |
| 15 | +First, you need to configure Next.js to [deploy static exports](https://nextjs.org/docs/app/building-your-application/deploying/static-exports). This is required for GitHub Pages to work. |
| 16 | + |
| 17 | +1. Open the `next.config.mjs` file |
| 18 | +2. Add the following: |
| 19 | + |
| 20 | +```js |
| 21 | +/** @type {import('next').NextConfig} */ |
| 22 | +const nextConfig = { |
| 23 | + /** |
| 24 | + * Enable static exports for the App Router. |
| 25 | + * |
| 26 | + * @see https://nextjs.org/docs/app/building-your-application/deploying/static-exports |
| 27 | + */ |
| 28 | + output: "export", |
| 29 | + |
| 30 | + /** |
| 31 | + * Set base path. This is the slug of your GitHub repository. |
| 32 | + * |
| 33 | + * @see https://nextjs.org/docs/app/api-reference/next-config-js/basePath |
| 34 | + */ |
| 35 | + basePath: "/nextjs-github-pages", |
| 36 | + |
| 37 | + /** |
| 38 | + * Disable server-based image optimization. Next.js does not support |
| 39 | + * dynamic features with static exports. |
| 40 | + * |
| 41 | + * @see https://nextjs.org/docs/app/api-reference/components/image#unoptimized |
| 42 | + */ |
| 43 | + images: { |
| 44 | + unoptimized: true, |
| 45 | + }, |
| 46 | +}; |
| 47 | + |
| 48 | +export default nextConfig; |
| 49 | +``` |
| 50 | + |
| 51 | +3. Save the `next.config.mjs` |
| 52 | + |
| 53 | +4. Finally, place a `.nojekyll` file in the `/public` directory to disable GitHub Pages from trying to create a [Jekyll](https://github.blog/2009-12-29-bypassing-jekyll-on-github-pages/) website. |
| 54 | + |
| 55 | +```treeview |
| 56 | +. |
| 57 | +├── app/ |
| 58 | +├── public/ |
| 59 | +│ └── .nojekyll |
| 60 | +├── next.config.js |
| 61 | +``` |
| 62 | + |
| 63 | +Perfect! This is all you need to configure Next.js to deploy on GitHub Pages. |
| 64 | + |
| 65 | +### Add base path to `page.tsx` |
| 66 | + |
| 67 | +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. |
| 68 | + |
| 69 | +1. Open `app/page.tsx` |
| 70 | +2. Find the `Image` components |
| 71 | +3. Add `/nextjs-github-pages/` (or the slug of your GitHub repository) to the `src` prop: |
| 72 | + |
| 73 | +```tsx[class="line-numbers"] |
| 74 | + <Image |
| 75 | + src="/nextjs-github-pages/vercel.svg" |
| 76 | + alt="Vercel Logo" |
| 77 | + className={styles.vercelLogo} |
| 78 | + width={100} |
| 79 | + height={24} |
| 80 | + priority |
| 81 | + /> |
| 82 | +``` |
| 83 | + |
| 84 | +4. Save the `page.tsx` file |
| 85 | + |
| 86 | +Learn more by reading the official documentation [for basePath and images](https://nextjs.org/docs/app/api-reference/next-config-js/basePath#images). |
| 87 | + |
| 88 | +--- |
| 89 | + |
| 90 | +## Configure GitHub Repository |
| 91 | + |
| 92 | +Next you need to configure Github for automated deployments via GitHub Actions. |
| 93 | + |
| 94 | +### Enable GitHub Pages |
| 95 | + |
| 96 | +The following settings use the [Github Action Deploy Pages](https://github.com/actions/deploy-pages) to deploy. I prefer this workflow because you don't need to generate SSH keys or use a personal access token. |
| 97 | + |
| 98 | +1. Go to your repository's **Settings** tab |
| 99 | +2. Click "Pages" in the sidebar |
| 100 | +3. Under "Build and Deployment", select "GitHub Actions" as the source: |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | +### Setup GitHub Action |
| 105 | + |
| 106 | +This is where the magic happens! This [workflow file](https://github.com/gregrickaby/nextjs-github-pages/blob/main/.github/workflows/deploy.yml) will automatically build and deploy the app when you push to the `main` branch. |
| 107 | + |
| 108 | +1. Create `.github/workflows/deploy.yml` file |
| 109 | +2. Paste the contents of <https://github.com/gregrickaby/nextjs-github-pages/blob/main/.github/workflows/deploy.yml> |
| 110 | +3. Save the `deploy.yml` file |
| 111 | + |
| 112 | +### Push to GitHub |
| 113 | + |
| 114 | +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. |
| 115 | + |
| 116 | +```bash |
| 117 | +git add . && git commit -m "Initial commit" && git push |
| 118 | +``` |
| 119 | + |
| 120 | +You should see your site deployed to GitHub Pages in a few minutes. 🚀 |
| 121 | + |
| 122 | +--- |
| 123 | + |
| 124 | +## Wrap up |
| 125 | + |
| 126 | +Thanks for reading and I hope this helps. If you noticed someting wrong, please [file an issue](https://github.com/gregrickaby/nextjs-github-pages/issues). Good luck! 🍻 |
| 127 | + |
| 128 | +--- |
0 commit comments