|
| 1 | +# GitHub Pages Deployment Guide |
| 2 | + |
| 3 | +This document explains the GitHub Pages configuration for the vizualni-admin Next.js application. |
| 4 | + |
| 5 | +## Configuration Overview |
| 6 | + |
| 7 | +### Repository Settings |
| 8 | +- **Repository**: acailic/improvements-ampl |
| 9 | +- **Base Path**: `/improvements-ampl/` (required for subdirectory deployment) |
| 10 | +- **Deployment Branch**: Configure in GitHub Settings > Pages |
| 11 | + |
| 12 | +### Next.js Configuration |
| 13 | + |
| 14 | +The application uses `next.config.static.js` with the following GitHub Pages-specific settings: |
| 15 | + |
| 16 | +```javascript |
| 17 | +basePath: '/improvements-ampl' |
| 18 | +assetPrefix: '/improvements-ampl/' |
| 19 | +output: 'export' |
| 20 | +trailingSlash: true |
| 21 | +images: { unoptimized: true } |
| 22 | +``` |
| 23 | + |
| 24 | +### Service Worker Status |
| 25 | + |
| 26 | +**Service Worker is DISABLED for GitHub Pages deployment.** |
| 27 | + |
| 28 | +Reasons: |
| 29 | +1. Service workers require proper HTTPS scope configuration |
| 30 | +2. GitHub Pages subdirectory deployments complicate service worker scope |
| 31 | +3. The service worker path would need to be `/improvements-ampl/sw.js` |
| 32 | +4. Service worker registration requires careful scope management |
| 33 | + |
| 34 | +If you need service worker functionality, consider: |
| 35 | +- Deploying to a custom domain (no subdirectory) |
| 36 | +- Using Vercel, Netlify, or similar platforms |
| 37 | +- Configuring service worker scope to match the basePath |
| 38 | + |
| 39 | +## Building for GitHub Pages |
| 40 | + |
| 41 | +### Local Build |
| 42 | + |
| 43 | +```bash |
| 44 | +# Set environment variable for GitHub Pages |
| 45 | +export GITHUB_PAGES=true |
| 46 | + |
| 47 | +# Build the application |
| 48 | +npm run build |
| 49 | + |
| 50 | +# Output will be in the 'out' directory |
| 51 | +``` |
| 52 | + |
| 53 | +### Build Script |
| 54 | + |
| 55 | +Create a `scripts/build-github-pages.sh`: |
| 56 | + |
| 57 | +```bash |
| 58 | +#!/bin/bash |
| 59 | +set -e |
| 60 | + |
| 61 | +echo "Building for GitHub Pages deployment..." |
| 62 | + |
| 63 | +# Set GitHub Pages environment |
| 64 | +export GITHUB_PAGES=true |
| 65 | + |
| 66 | +# Clean previous build |
| 67 | +rm -rf out |
| 68 | + |
| 69 | +# Build the static site |
| 70 | +npm run build |
| 71 | + |
| 72 | +# Create .nojekyll file to prevent Jekyll processing |
| 73 | +touch out/.nojekyll |
| 74 | + |
| 75 | +# Optional: Add custom 404 page handling |
| 76 | +cp out/404.html out/404/index.html 2>/dev/null || true |
| 77 | + |
| 78 | +echo "Build complete! Output in ./out directory" |
| 79 | +echo "Deploy the 'out' directory to GitHub Pages" |
| 80 | +``` |
| 81 | + |
| 82 | +### Package.json Scripts |
| 83 | + |
| 84 | +Add these scripts to your `package.json`: |
| 85 | + |
| 86 | +```json |
| 87 | +{ |
| 88 | + "scripts": { |
| 89 | + "build:github": "GITHUB_PAGES=true next build", |
| 90 | + "export": "next export", |
| 91 | + "deploy:github": "npm run build:github && npm run export" |
| 92 | + } |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +## GitHub Actions Workflow |
| 97 | + |
| 98 | +Create `.github/workflows/deploy-github-pages.yml`: |
| 99 | + |
| 100 | +```yaml |
| 101 | +name: Deploy to GitHub Pages |
| 102 | + |
| 103 | +on: |
| 104 | + push: |
| 105 | + branches: [main] |
| 106 | + workflow_dispatch: |
| 107 | + |
| 108 | +permissions: |
| 109 | + contents: read |
| 110 | + pages: write |
| 111 | + id-token: write |
| 112 | + |
| 113 | +concurrency: |
| 114 | + group: "pages" |
| 115 | + cancel-in-progress: false |
| 116 | + |
| 117 | +jobs: |
| 118 | + build: |
| 119 | + runs-on: ubuntu-latest |
| 120 | + steps: |
| 121 | + - name: Checkout |
| 122 | + uses: actions/checkout@v4 |
| 123 | + |
| 124 | + - name: Setup Node.js |
| 125 | + uses: actions/setup-node@v4 |
| 126 | + with: |
| 127 | + node-version: '20' |
| 128 | + cache: 'npm' |
| 129 | + |
| 130 | + - name: Install dependencies |
| 131 | + run: npm ci |
| 132 | + |
| 133 | + - name: Build with Next.js |
| 134 | + env: |
| 135 | + GITHUB_PAGES: true |
| 136 | + run: npm run build |
| 137 | + |
| 138 | + - name: Upload artifact |
| 139 | + uses: actions/upload-pages-artifact@v3 |
| 140 | + with: |
| 141 | + path: ./out |
| 142 | + |
| 143 | + deploy: |
| 144 | + environment: |
| 145 | + name: github-pages |
| 146 | + url: ${{ steps.deployment.outputs.page_url }} |
| 147 | + runs-on: ubuntu-latest |
| 148 | + needs: build |
| 149 | + steps: |
| 150 | + - name: Deploy to GitHub Pages |
| 151 | + id: deployment |
| 152 | + uses: actions/deploy-pages@v4 |
| 153 | +``` |
| 154 | +
|
| 155 | +## GitHub Pages Settings |
| 156 | +
|
| 157 | +1. Go to your repository settings |
| 158 | +2. Navigate to **Pages** section |
| 159 | +3. Configure: |
| 160 | + - **Source**: GitHub Actions (recommended) OR Deploy from a branch |
| 161 | + - **Branch**: main (if using branch deployment) |
| 162 | + - **Folder**: /out or /root |
| 163 | +
|
| 164 | +## Asset Path Handling |
| 165 | +
|
| 166 | +All assets are automatically prefixed with `/improvements-ampl/`: |
| 167 | + |
| 168 | +- Images: `/improvements-ampl/images/logo.png` |
| 169 | +- Scripts: `/improvements-ampl/_next/static/...` |
| 170 | +- Styles: `/improvements-ampl/_next/static/css/...` |
| 171 | + |
| 172 | +### Using Links in Code |
| 173 | + |
| 174 | +```jsx |
| 175 | +// Next.js Link component (automatically handles basePath) |
| 176 | +import Link from 'next/link'; |
| 177 | +<Link href="/about">About</Link> |
| 178 | +
|
| 179 | +// Next.js Image component (automatically handles basePath) |
| 180 | +import Image from 'next/image'; |
| 181 | +<Image src="/logo.png" alt="Logo" width={100} height={100} /> |
| 182 | +
|
| 183 | +// Manual links (use basePath) |
| 184 | +const basePath = process.env.NODE_ENV === 'production' ? '/improvements-ampl' : ''; |
| 185 | +<a href={`${basePath}/static/file.pdf`}>Download</a> |
| 186 | +``` |
| 187 | + |
| 188 | +## Troubleshooting |
| 189 | + |
| 190 | +### 404 Errors on Page Refresh |
| 191 | + |
| 192 | +**Issue**: Direct navigation to routes shows 404 |
| 193 | +**Solution**: Enable `trailingSlash: true` in next.config.js (already configured) |
| 194 | + |
| 195 | +### Missing Assets (404 for CSS/JS) |
| 196 | + |
| 197 | +**Issue**: Assets return 404 |
| 198 | +**Solution**: Ensure `assetPrefix` and `basePath` are set correctly |
| 199 | + |
| 200 | +### Service Worker 404 Error |
| 201 | + |
| 202 | +**Issue**: `Failed to register ServiceWorker` |
| 203 | +**Solution**: Service worker is disabled in pages/_app.tsx for static deployment |
| 204 | + |
| 205 | +### Images Not Loading |
| 206 | + |
| 207 | +**Issue**: Images show broken |
| 208 | +**Solution**: Ensure `images: { unoptimized: true }` is set (required for static export) |
| 209 | + |
| 210 | +## Local Testing |
| 211 | + |
| 212 | +To test the GitHub Pages build locally: |
| 213 | + |
| 214 | +```bash |
| 215 | +# Build for GitHub Pages |
| 216 | +GITHUB_PAGES=true npm run build |
| 217 | + |
| 218 | +# Serve the out directory |
| 219 | +npx serve out -p 3000 |
| 220 | + |
| 221 | +# Visit http://localhost:3000/improvements-ampl/ |
| 222 | +``` |
| 223 | + |
| 224 | +Or use the `basePath` in development: |
| 225 | + |
| 226 | +```bash |
| 227 | +# Start dev server with basePath |
| 228 | +GITHUB_PAGES=true npm run dev |
| 229 | + |
| 230 | +# Visit http://localhost:3000/improvements-ampl/ |
| 231 | +``` |
| 232 | + |
| 233 | +## Files Changed for GitHub Pages |
| 234 | + |
| 235 | +1. **next.config.static.js**: Added basePath, assetPrefix, output, trailingSlash |
| 236 | +2. **pages/_app.tsx**: Created to disable service worker registration |
| 237 | +3. **.nojekyll**: Prevents Jekyll processing |
| 238 | +4. **public/.nojekyll**: Additional Jekyll bypass |
| 239 | + |
| 240 | +## Deployment Checklist |
| 241 | + |
| 242 | +- [ ] Set `GITHUB_PAGES=true` environment variable |
| 243 | +- [ ] Run `npm run build` successfully |
| 244 | +- [ ] Verify `out` directory contains all files |
| 245 | +- [ ] Check that `out/.nojekyll` exists |
| 246 | +- [ ] Deploy `out` directory to GitHub Pages |
| 247 | +- [ ] Test deployment URL: https://acailic.github.io/improvements-ampl/ |
| 248 | +- [ ] Verify all assets load correctly |
| 249 | +- [ ] Test navigation between pages |
| 250 | +- [ ] Confirm no service worker errors in console |
| 251 | + |
| 252 | +## Alternative Deployment Options |
| 253 | + |
| 254 | +If GitHub Pages subdirectory deployment is problematic, consider: |
| 255 | + |
| 256 | +1. **Custom Domain**: Deploy to root of custom domain |
| 257 | +2. **Vercel**: `vercel --prod` (automatic Next.js optimization) |
| 258 | +3. **Netlify**: Drag and drop `out` folder |
| 259 | +4. **GitHub Pages with Custom Domain**: No basePath needed |
| 260 | +5. **Cloudflare Pages**: Direct GitHub integration |
| 261 | + |
| 262 | +## Resources |
| 263 | + |
| 264 | +- [Next.js Static Export](https://nextjs.org/docs/app/building-your-application/deploying/static-exports) |
| 265 | +- [GitHub Pages Documentation](https://docs.github.com/en/pages) |
| 266 | +- [Next.js basePath Configuration](https://nextjs.org/docs/app/api-reference/next-config-js/basePath) |
0 commit comments