Lädt...


🔧 6 steps to deploy your React Next.js app with Github pages


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

Deploying a Next.js app to GitHub Pages can be a bit tricky due to the static nature of GitHub Pages and the dynamic features of Next.js. In this article, I'll walk you through the steps to successfully deploy do it.

Prerequisites

  • A GitHub account
  • Node.js and npm installed on your machine
  • A Next.js project ready to deploy

Step 1: Install gh-pages

npm install gh-pages --save-dev

Step 2: Update next.config.mjs

Next, you need to update your next.config.js (or next.config.mjs) file to handle the base path and asset prefix correctly.

const isProd = process.env.NODE_ENV === 'production';
const nextConfig = {
  reactStrictMode: true,
  images: {
    unoptimized: true, // Disable default image optimization
  },
  assetPrefix: isProd ? '/your-repository-name/' : '',
  basePath: isProd ? '/your-repository-name' : '',
  output: 'export'
};

export default nextConfig;

isProd checks if the NODE_ENV environment variable is set to 'production'. If it is, isProd will be true; otherwise, it will be false.
Conditional assetPrefix and basePath are set to your repository name only if isProd is true. Otherwise, they are set to an empty string for local development.
The images.unoptimized option is set to true to disable the default image optimization, which is not compatible with static exports.

Step 3: Update package.json

Update your package.json to include the homepage field and the deploy scripts.

"homepage": "https://<your-github-username>.github.io/<your-repo-name>",
  "private": true,
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint",
    "predeploy": "npm run build",
    "deploy": "gh-pages -d out"
  },

Replace and with your GitHub username and repository name.

Step 4: Deploy Your App

Run npm run predeploy and npm run deploy

These commands will:

  • Build your project.
  • Export it to the out directory.
  • Deploy it to the gh-pages branch on GitHub. Push your work to your GitHub branch before the next step

Step 5: Configure GitHub Pages

Go to your repository on GitHub.
Navigate to Settings > Pages.
Under Source, select the gh-pages branch.
Save the settings.
Add manually the .nojekyll file at the root of the gh-pages branch on GitHub. Learn more about this here.

Step 6: Verify Deployment

After deploying, open your GitHub Pages URL (e.g., https://.github.io/) to verify that your app is live.

And voila!

Feel free to leave any questions or comments below. Happy coding!

...

🔧 6 steps to deploy your React Next.js app with Github pages


📈 51.2 Punkte
🔧 Programmierung

🔧 How to Deploy Your React or Vite Project on GitHub Pages using gh-pages


📈 44.3 Punkte
🔧 Programmierung

🔧 Host NextJS for Free on GitHub Pages || Deploying a Next.js (App Router) Application to GitHub Pages


📈 40.34 Punkte
🔧 Programmierung

🔧 Deploy React App to GitHub Pages using GitHub Actions


📈 40.3 Punkte
🔧 Programmierung

🔧 How to deploy to GitHub Pages using gh-pages package


📈 35.5 Punkte
🔧 Programmierung

🔧 How to Deploy an Express.js App on GitHub Pages Using GitHub Actions


📈 34.32 Punkte
🔧 Programmierung

🔧 Deploy Nextjs app to github-pages with Github Actions


📈 34.32 Punkte
🔧 Programmierung

🔧 Deploy a ReactJS App with ViteJS to GitHub Pages using GitHub Actions | Step-by-Step Tutorial


📈 34.32 Punkte
🔧 Programmierung

🔧 How to Deploy a React App to GitHub Pages


📈 34.08 Punkte
🔧 Programmierung

🔧 How to deploy React App to GitHub Pages - Caveman Style 🌋 🔥🦴


📈 34.08 Punkte
🔧 Programmierung

🔧 Build a Loan EMI Calculator in React and Deploy it on GitHub Pages


📈 31.49 Punkte
🔧 Programmierung

🔧 How to deploy React Portfolio|Project with Github Pages as @ 2024


📈 31.49 Punkte
🔧 Programmierung

🔧 Deploy a React application with multiple pages to Github


📈 31.49 Punkte
🔧 Programmierung

🔧 Deploy React Vite PWA to GitHub Pages


📈 31.49 Punkte
🔧 Programmierung

🔧 Deploying a Vite app on GitHub Pages using GitHub Actions with GitHub Secrets


📈 31.23 Punkte
🔧 Programmierung

🔧 GitHub Actions CI/CD for React app hosted in GitHub Pages


📈 30.99 Punkte
🔧 Programmierung

🔧 How to Deploy Next.js Apps to Github Pages


📈 30.85 Punkte
🔧 Programmierung

🔧 How to Deploy Your React App Using Cloudflare Pages, Vercel, and Netlify


📈 30.68 Punkte
🔧 Programmierung

🔧 This Week In React #185: React Conf, React Query, refs, Next.js after, mini-react...


📈 29.26 Punkte
🔧 Programmierung

🔧 This Week In React #185: React Conf, React Query, refs, Next.js after, mini-react...


📈 29.26 Punkte
🔧 Programmierung

🔧 Deploying React Apps to Github Pages with Github Actions


📈 28.41 Punkte
🔧 Programmierung

🔧 Deploy your own Linktree with LittleLink on GitHub Pages


📈 28.32 Punkte
🔧 Programmierung

🔧 How to easily deploy your Angular Application to GitHub Pages


📈 28.32 Punkte
🔧 Programmierung

🔧 Deploy your portfolio using GitHub pages


📈 28.32 Punkte
🔧 Programmierung

🔧 Deploy a solid-start app on github pages


📈 28.1 Punkte
🔧 Programmierung

🔧 How to Quickly Deploy Your Angular/React/Vue Website for Free Using EdgeOne Pages


📈 28.09 Punkte
🔧 Programmierung

🔧 React + Next.js + tailwind Github pages


📈 27.53 Punkte
🔧 Programmierung

🔧 React + Next.js + tailwind Github pages


📈 27.53 Punkte
🔧 Programmierung

🔧 React + Next.js + tailwind Github pages


📈 27.53 Punkte
🔧 Programmierung

🔧 Deno Deploy: Deploy a React App


📈 27.18 Punkte
🔧 Programmierung

🔧 5 Steps You Must Follow to Deploy a React App on Hostinger


📈 26.85 Punkte
🔧 Programmierung

🔧 5 Steps You Must Follow to Deploy a React App on Hostinger


📈 26.85 Punkte
🔧 Programmierung

🔧 Deploy React App to Google App Engine with Github Actions CI/CD - A Complete Guide


📈 26.68 Punkte
🔧 Programmierung

matomo