|
| 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 | +> ⚠️ 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). |
| 6 | +
|
| 7 | +--- |
| 8 | + |
| 9 | +## Configure Next.js |
| 10 | + |
| 11 | +### Next.js Config |
| 12 | + |
| 13 | +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. |
| 14 | + |
| 15 | +1. Open the `next.config.ts` file |
| 16 | +2. Add the following: |
| 17 | + |
| 18 | +```typescript |
| 19 | +import type { NextConfig } from "next"; |
| 20 | + |
| 21 | +const nextConfig: NextConfig = { |
| 22 | + /** |
| 23 | + * Enable static exports. |
| 24 | + * |
| 25 | + * @see https://nextjs.org/docs/app/building-your-application/deploying/static-exports |
| 26 | + */ |
| 27 | + output: "export", |
| 28 | + |
| 29 | + /** |
| 30 | + * Set base path. This is the slug of your GitHub repository. |
| 31 | + * |
| 32 | + * @see https://nextjs.org/docs/app/api-reference/next-config-js/basePath |
| 33 | + */ |
| 34 | + basePath: "/nextjs-github-pages", |
| 35 | + |
| 36 | + /** |
| 37 | + * Disable server-based image optimization. Next.js does not support |
| 38 | + * dynamic features with static exports. |
| 39 | + * |
| 40 | + * @see https://nextjs.org/docs/app/api-reference/components/image#unoptimized |
| 41 | + */ |
| 42 | + images: { |
| 43 | + unoptimized: true, |
| 44 | + }, |
| 45 | +}; |
| 46 | + |
| 47 | +export default nextConfig; |
| 48 | +``` |
| 49 | + |
| 50 | +3. Save the `next.config.ts` |
| 51 | + |
| 52 | +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. |
| 53 | + |
| 54 | +```treeview |
| 55 | +. |
| 56 | +├── app/ |
| 57 | +├── public/ |
| 58 | +│ └── .nojekyll |
| 59 | +├── next.config.ts |
| 60 | +``` |
| 61 | + |
| 62 | +Perfect! This is all you need to configure Next.js to deploy on GitHub Pages. |
| 63 | + |
| 64 | +### Add base path to `page.tsx` |
| 65 | + |
| 66 | +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. |
| 67 | + |
| 68 | +1. Open `app/page.tsx` |
| 69 | +2. Find the `Image` components |
| 70 | +3. Add `/nextjs-github-pages/` (or the slug of your GitHub repository) to the `src` prop: |
| 71 | + |
| 72 | +```tsx |
| 73 | +<Image |
| 74 | + src="/nextjs-github-pages/vercel.svg" |
| 75 | + alt="Vercel Logo" |
| 76 | + className={styles.vercelLogo} |
| 77 | + width={100} |
| 78 | + height={24} |
| 79 | + priority |
| 80 | +/> |
| 81 | +``` |
| 82 | + |
| 83 | +4. Save the `page.tsx` file |
| 84 | + |
| 85 | +Learn more by reading the official documentation [for basePath and images](https://nextjs.org/docs/app/api-reference/config/next-config-js/basePath#images). |
| 86 | + |
| 87 | +--- |
| 88 | + |
| 89 | +## Configure GitHub Repository |
| 90 | + |
| 91 | +Now that Next.js is configured, you need to set up your GitHub repository to deploy to GitHub Pages. |
| 92 | + |
| 93 | +### Setup GitHub Action |
| 94 | + |
| 95 | +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. |
| 96 | + |
| 97 | +1. Create `.github/workflows/deploy.yml` file |
| 98 | +2. Paste the contents of <https://github.com/gregrickaby/nextjs-github-pages/blob/main/.github/workflows/deploy.yml> |
| 99 | +3. Save the `deploy.yml` file |
| 100 | + |
| 101 | +### Enable GitHub Pages |
| 102 | + |
| 103 | +1. Go to your repository's **Settings** tab |
| 104 | +2. Click "Pages" in the sidebar |
| 105 | +3. Under "Build and Deployment", select "GitHub Actions" as the source: |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | +### Push to GitHub |
| 110 | + |
| 111 | +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. |
| 112 | + |
| 113 | +```bash |
| 114 | +git add . && git commit -m "initial commit" && git push |
| 115 | +``` |
| 116 | + |
| 117 | +You should see your site deployed to GitHub Pages in a few minutes. 🚀 |
| 118 | + |
| 119 | +--- |
| 120 | + |
| 121 | +## Wrap up |
| 122 | + |
| 123 | +Thanks for reading and I hope this helps. If you noticed someting wrong, please [open an issue](https://github.com/gregrickaby/nextjs-github-pages/issues). Cheers! 🍻 |
| 124 | + |
| 125 | +--- |
0 commit comments