Next.js 部署到 GitHub Pages 教程

Next.js 部署到 GitHub Pages 教程

nextjs-github-pages🚀 Deploy a Next.js app to Github Pages via Github Actions.项目地址:https://gitcode.com/gh_mirrors/ne/nextjs-github-pages

项目介绍

nextjs-github-pages 是一个开源项目,旨在通过 GitHub Actions 将 Next.js 应用部署到 GitHub Pages。该项目支持 Next.js 的 App Router,并提供了详细的配置指南和最佳实践。

项目快速启动

克隆项目

首先,克隆项目到本地:

git clone https://github.com/gregrickaby/nextjs-github-pages.git
cd nextjs-github-pages

安装依赖

安装项目依赖:

npm install

配置 Next.js

编辑 next.config.js 文件,添加以下配置:

/** @type {import('next').NextConfig} */
const nextConfig = {
  output: "export",
  basePath: "/nextjs-github-pages",
  images: {
    unoptimized: true,
  },
};

module.exports = nextConfig;

添加 .nojekyll 文件

public 目录下添加 .nojekyll 文件,以禁用 GitHub Pages 的 Jekyll 构建:

touch public/.nojekyll

部署到 GitHub Pages

  1. 创建 GitHub Actions 配置文件 deploy.yml
name: Deploy to GitHub Pages
on:
  push:
    branches:
      - main
jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v2
        with:
          node-version: '14'
      - run: npm install
      - run: npm run build
      - run: npm run export
      - name: Deploy
        uses: peaceiris/actions-gh-pages@v3
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          publish_dir: ./out
  1. 提交代码并推送到 GitHub:
git add .
git commit -m "Initial commit"
git push origin main

应用案例和最佳实践

应用案例

  • 静态网站:使用 Next.js 构建的静态网站可以轻松部署到 GitHub Pages,适用于个人博客、文档站点等。
  • 开源项目文档:许多开源项目使用 Next.js 构建文档站点,并通过 GitHub Pages 进行部署。

最佳实践

  • 优化图片:虽然 GitHub Pages 不支持 Next.js 的图片优化功能,但可以通过其他工具(如 ImageOptim)预先优化图片。
  • 使用 GitHub Actions:利用 GitHub Actions 自动化部署流程,确保每次提交都能自动部署到 GitHub Pages。

典型生态项目

  • Next.js:一个用于构建服务器渲染的 React 应用程序的框架。
  • GitHub Actions:GitHub 提供的自动化工作流工具,用于构建、测试和部署代码。
  • GitHub Pages:GitHub 提供的静态网站托管服务,适用于托管静态网页和项目文档。

通过以上步骤和最佳实践,您可以轻松地将 Next.js 应用部署到 GitHub Pages,并享受自动化部署带来的便利。

nextjs-github-pages🚀 Deploy a Next.js app to Github Pages via Github Actions.项目地址:https://gitcode.com/gh_mirrors/ne/nextjs-github-pages

  • 17
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

戴艺音

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值