|
1 |
| -This is a [Next.js](https://nextjs.org) project bootstrapped with [`create-next-app`](https://nextjs.org/docs/app/api-reference/cli/create-next-app). |
| 1 | +## About This Repository |
2 | 2 |
|
3 |
| -## Getting Started |
| 3 | +This repository contains a simple step-by-step guide for deploying a **Next.js project** to **GitHub Pages**. It is designed for developers looking to host static Next.js sites with minimal configuration. |
4 | 4 |
|
5 |
| -First, run the development server: |
| 5 | +By following this guide, you can deploy your Next.js app to GitHub Pages with ease. Feel free to contribute or open issues if you face any problems. |
6 | 6 |
|
7 |
| -```bash |
8 |
| -npm run dev |
9 |
| -# or |
10 |
| -yarn dev |
11 |
| -# or |
12 |
| -pnpm dev |
13 |
| -# or |
14 |
| -bun dev |
| 7 | +# Deploy Next.js on GitHub Pages 🚀 |
| 8 | + |
| 9 | +This guide provides step-by-step instructions to **deploy a Next.js project** to **GitHub Pages** seamlessly. |
| 10 | + |
| 11 | +--- |
| 12 | + |
| 13 | +## 🚀 Prerequisites |
| 14 | + |
| 15 | +Before proceeding, ensure you have: |
| 16 | + |
| 17 | +- **Node.js (LTS version)** |
| 18 | +- **GitHub repository** created and set to public |
| 19 | +- **Basic knowledge of Git & GitHub** |
| 20 | + |
| 21 | +--- |
| 22 | + |
| 23 | +## Step 1: Create a Next.js Project (Skip if Already Done) |
| 24 | + |
| 25 | +If you haven't created a Next.js project yet, run: |
| 26 | + |
| 27 | +```sh |
| 28 | +npx create-next-app@latest my-next-app |
| 29 | +cd my-next-app |
| 30 | +``` |
| 31 | + |
| 32 | +If you already have a project, **skip this step**. |
| 33 | + |
| 34 | +--- |
| 35 | + |
| 36 | +## Step 2: Check & Fix Linting Issues (Important) |
| 37 | + |
| 38 | +Before deployment, ensure there are **no linting errors** by running: |
| 39 | + |
| 40 | +```sh |
| 41 | +npm run lint -- --fix |
| 42 | +``` |
| 43 | + |
| 44 | +or |
| 45 | + |
| 46 | +```sh |
| 47 | +npx eslint . --fix |
| 48 | +``` |
| 49 | + |
| 50 | +✅ **Make sure there are no errors before proceeding!** |
| 51 | + |
| 52 | +--- |
| 53 | + |
| 54 | +## Step 3: Modify `next.config.js` and `package.json` |
| 55 | + |
| 56 | +### Edit `next.config.js` (or `next.config.ts` for TypeScript) |
| 57 | + |
| 58 | +Add the following configuration: |
| 59 | + |
| 60 | +```js |
| 61 | +const isProd = process.env.NODE_ENV === "production"; |
| 62 | + |
| 63 | +const nextConfig = { |
| 64 | + reactStrictMode: true, |
| 65 | + images: { |
| 66 | + unoptimized: true, // GitHub Pages doesn't support Next.js Image Optimization |
| 67 | + }, |
| 68 | + basePath: isProd ? "/<your-repo-name>" : "", |
| 69 | + assetPrefix: isProd ? "/<your-repo-name>/" : "", |
| 70 | +}; |
| 71 | + |
| 72 | +module.exports = nextConfig; |
| 73 | +``` |
| 74 | + |
| 75 | +**✅ Ensure that `basePath` and `assetPrefix` match your repository name.** |
| 76 | + |
| 77 | +### Update `package.json` Scripts |
| 78 | + |
| 79 | +Modify your `package.json` to include: |
| 80 | + |
| 81 | +```json |
| 82 | +"scripts": { |
| 83 | + "dev": "next dev", |
| 84 | + "build": "next build", |
| 85 | + "export": "next export", |
| 86 | + "deploy": "next build && next export && touch out/.nojekyll && gh-pages -d out" |
| 87 | +} |
| 88 | +``` |
| 89 | + |
| 90 | +--- |
| 91 | + |
| 92 | +## Step 4: Install Required Dependencies |
| 93 | + |
| 94 | +Run the following command to install `gh-pages`: |
| 95 | + |
| 96 | +```sh |
| 97 | +npm install gh-pages |
| 98 | +``` |
| 99 | + |
| 100 | +--- |
| 101 | + |
| 102 | +## Step 5: Update Image Paths |
| 103 | + |
| 104 | +To ensure images load correctly, **update all image paths**: |
| 105 | +If your images are inside the `public/images/` folder, use: |
| 106 | + |
| 107 | +```jsx |
| 108 | +<img src="/<your-repo-name>/images/logo.png" alt="Logo" /> |
| 109 | +``` |
| 110 | + |
| 111 | +✅ **Sometimes the following works, but if images don’t load, use the first approach:** |
| 112 | + |
| 113 | +```jsx |
| 114 | +<img src="/images/logo.png" alt="Logo" /> |
15 | 115 | ```
|
16 | 116 |
|
17 |
| -Open [http://localhost:3000](http://localhost:3000) with your browser to see the result. |
| 117 | +--- |
| 118 | + |
| 119 | +## Step 6: Push Your Code to GitHub |
| 120 | + |
| 121 | +Commit and push your changes to your GitHub repository: |
| 122 | + |
| 123 | +```sh |
| 124 | +git add . |
| 125 | +git commit -m "Configured Next.js for GitHub Pages" |
| 126 | +git push origin main |
| 127 | +``` |
| 128 | + |
| 129 | +--- |
| 130 | + |
| 131 | +## Step 7: Open GitHub Repository |
| 132 | + |
| 133 | +1. Go to **GitHub** and open your repository. |
| 134 | +2. Click on **Settings** (for the repository, NOT your profile). |
| 135 | + |
| 136 | +--- |
| 137 | + |
| 138 | +## Step 8: Configure GitHub Pages |
| 139 | + |
| 140 | +1. **Scroll down** to the **Pages** section. |
| 141 | +2. Under **Build and Deployment**, select **GitHub Actions**. |
| 142 | +3. Click **Configure**. |
| 143 | + |
| 144 | +--- |
| 145 | + |
| 146 | +## Step 9: Follow These Image Instructions |
| 147 | + |
| 148 | + |
| 149 | +_(Add step-by-step images/screenshots here to help users configure GitHub Actions correctly.)_ |
| 150 | + |
| 151 | +--- |
| 152 | + |
| 153 | +## Step 10: Deploy Your Project |
| 154 | + |
| 155 | +After GitHub Actions runs successfully, visit: |
| 156 | +**`https://<your-username>.github.io/<your-repo-name>/`** |
| 157 | + |
| 158 | +--- |
18 | 159 |
|
19 |
| -You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file. |
| 160 | +## ✅ Additional Notes |
20 | 161 |
|
21 |
| -This project uses [`next/font`](https://nextjs.org/docs/app/building-your-application/optimizing/fonts) to automatically optimize and load [Geist](https://vercel.com/font), a new font family for Vercel. |
| 162 | +- If images **don’t load**, ensure you’ve set `basePath` and `assetPrefix` correctly. |
| 163 | +- If you get **404 errors**, recheck the GitHub Pages settings. |
| 164 | +- To redeploy after updates, simply run: |
| 165 | + ```sh |
| 166 | + npm run deploy |
| 167 | + ``` |
22 | 168 |
|
23 |
| -## Learn More |
| 169 | +--- |
24 | 170 |
|
25 |
| -To learn more about Next.js, take a look at the following resources: |
| 171 | +## 📌 Alternative: Deploying on Vercel |
26 | 172 |
|
27 |
| -- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API. |
28 |
| -- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial. |
| 173 | +For easier deployment, use **Vercel**: |
29 | 174 |
|
30 |
| -You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js) - your feedback and contributions are welcome! |
| 175 | +1. Sign in to [Vercel](https://vercel.com/) |
| 176 | +2. Connect your GitHub repo |
| 177 | +3. Click **Deploy** (No extra configuration needed!) |
31 | 178 |
|
32 |
| -## Deploy on Vercel |
| 179 | +--- |
33 | 180 |
|
34 |
| -The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js. |
| 181 | +This guide ensures a smooth **Next.js deployment on GitHub Pages** without errors. 🚀 If you found this helpful, **star this repo!** ⭐ |
35 | 182 |
|
36 |
| -Check out our [Next.js deployment documentation](https://nextjs.org/docs/app/building-your-application/deploying) for more details. |
| 183 | +--- |
0 commit comments