404 on _next资产在部署到Github Pages之后

kb5ga3dv  于 2023-01-05  发布在  Git
关注(0)|答案(1)|浏览(243)

我尝试将我的下一个应用程序部署到gh页面,但是只有索引和404页面显示,所有其他页面、图片、js和css文件都返回了404错误-所有内容都在_next文件夹中。
经过一番研究,我发现这个问题的一个流行的解决方案是在输出文件夹中添加一个.nojekyll文件,以防止jekyll忽略前缀为_的文件,我已经尝试过了,但这些文件仍然丢失。
以下是我构建和部署应用程序的主要行动-

name: Deploy Next.js site to Pages

on:
  # Runs on pushes targeting the default branch
  push:
    branches: ["main"]

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
permissions:
  contents: read
  pages: write
  id-token: write

# Allow one concurrent deployment
concurrency:
  group: "pages"
  cancel-in-progress: true

jobs:
  # Build job
  build:
    runs-on: ubuntu-latest
    defaults:
          run:
            working-directory: ./docs
    steps:
      - name: Checkout
        uses: actions/checkout@v3
      - name: Detect package manager
        id: detect-package-manager
        run: |
          if [ -f "${{ github.workspace }}/docs/yarn.lock" ]; then
            echo "::set-output name=manager::yarn"
            echo "::set-output name=command::install"
            echo "::set-output name=runner::yarn"
            exit 0
          elif [ -f "${{ github.workspace }}/docs/package.json" ]; then
            echo "::set-output name=manager::npm"
            echo "::set-output name=command::ci"
            echo "::set-output name=runner::npx --no-install"
            exit 0
          else
            echo "Unable to determine packager manager"
            exit 1
          fi
      - name: Setup Node
        uses: actions/setup-node@v3
        with:
          node-version: "16"
          cache: ${{ steps.detect-package-manager.outputs.manager }}
      - name: Restore cache
        uses: actions/cache@v3
        with:
          path: |
            .next/cache
          # Generate a new cache whenever packages or source files change.
          key: ${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json', '**/yarn.lock') }}-${{ hashFiles('**.[jt]s', '**.[jt]sx') }}
          # If source files changed but packages didn't, rebuild from a prior cache.
          restore-keys: |
            ${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json', '**/yarn.lock') }}-
      - name: Install dependencies
        run: ${{ steps.detect-package-manager.outputs.manager }} ${{ steps.detect-package-manager.outputs.command }}
      - name: Build with Next.js
        run: ${{ steps.detect-package-manager.outputs.runner }} next build
      - name: Static HTML export with Next.js
        run: ${{ steps.detect-package-manager.outputs.runner }} next export
      - name: nojekyll
        run: touch ./out/.nojekyll
      - name: Upload artifact
        uses: actions/upload-pages-artifact@v1
        with:
          path: ./docs/out

  # Deployment job
  deploy:
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    runs-on: ubuntu-latest
    needs: build
    steps:
      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@v1

我已经检查了部署的工件,所有预期的文件都在那里。
我错过了什么?

z2acfund

z2acfund1#

在公用文件夹中添加文件. nojekyll
然后在package.json中添加

"deploy": "gh-pages -d dist -t true"

然后运行以下命令

yarn generate
yarn deploy

此文件. nojekyll将被复制到dist文件夹中
我使用nuxt应用程序,但同样的想法
谢谢

相关问题