通过 100 多个技巧学习 Nuxt!
部署

GitHub Pages

将您的 Nuxt 应用程序部署到 GitHub Pages 基础设施。

Nuxt 支持以最少的配置部署到 GitHub Pages

GitHub Pages 仅支持静态站点,Nuxt 会将您的应用程序预渲染为静态 HTML 文件。
如果您使用自定义域名,则需要在构建步骤中将 NUXT_APP_BASE_URL 设置为您的存储库 slug。示例https://<user>.github.io/<repository>/NUXT_APP_BASE_URL=/<repository>/ npx nuxt build --preset github_pages

设置

按照步骤 创建一个 GitHub Pages 站点

部署

这是一个示例 GitHub Actions 工作流程,使用 github_pages 预设将您的站点部署到 GitHub Pages

.github/workflows/deploy.yml
# https://github.com/actions/deploy-pages#usage
name: Deploy to GitHub Pages
on:
  workflow_dispatch:
  push:
    branches:
      - main
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - run: corepack enable
      - uses: actions/setup-node@v4
        with:
          node-version: "20"
      # Pick your own package manager and build script
      - run: npm install
      - run: npx nuxt build --preset github_pages
      - name: Upload artifact
        uses: actions/upload-pages-artifact@v3
        with:
          path: ./.output/public
  # Deployment job
  deploy:
    # Add a dependency to the build job
    needs: build
    # Grant GITHUB_TOKEN the permissions required to make a Pages deployment
    permissions:
      pages: write      # to deploy to Pages
      id-token: write   # to verify the deployment originates from an appropriate source
    # Deploy to the github_pages environment
    environment:
      name: github_pages
      url: ${{ steps.deployment.outputs.page_url }}
    # Specify runner + deployment step
    runs-on: ubuntu-latest
    steps:
      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@v4
前往 Nitro 文档 了解有关 github-pages 部署预设的更多信息。