Search code examples
githubgithub-actionsgithub-pages

deploying react app in git workflow with updating base path as repo name


deploying to gh-pages using workflow having base url issue. actually my dist folder deployed under https://3gwebtrain.github.io/Ultimate-React-23/ git path with repo name. but after the deployment, my app searching the files from https://3gwebtrain.github.io/ that means without including my repo name. so my pages are blank.

please check here: https://3gwebtrain.github.io/Ultimate-React-23/ Is there a way to git deployment to know the correct directory to fetch the files from updating the workflow?

here is my current workflow:

name: React Build

on:
  push:
    branches: ['main']

env:
  CI: false

jobs:
  build:
    runs-on: ubuntu-latest
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    permissions:
      contents: 'read'
      id-token: 'write'
      pages: 'write'
      actions: 'write'
      checks: 'write'
      deployments: 'write'
    strategy:
      matrix:
        node-version: [18.x]

    steps:
      - uses: actions/checkout@v3

      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node-version }}

      - name: Build
        run: |
          npm install
          npm run build

      - name: Setup Pages
        uses: actions/configure-pages@v2
      - name: Upload artifact
        uses: actions/upload-pages-artifact@v1
        with:
          # Upload build directory content
          path: 'dist/'
      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

as a simple I would like to navigate the assets from https://3gwebtrain.github.io/assets/index-cef14fe5.js in to https://3gwebtrain.github.io/Ultimate-React-23/assets/index-cef14fe5.js


Solution

  • I fixed the issue by using the base name with BrowserRouter

    <BrowserRouter basename={import.meta.env.BASE_URL}>
      <Routes>
        <Route path='/' element={<HomePage />} />
        <Route path='product' element={<Product />} />
        <Route path='pricing' element={<Pricing />} />
        <Route path='app' element={<AppLayout />} />
        <Route path='*' element={<Pagenotfound />} />
      </Routes>
    </BrowserRouter>