Search code examples
fluttergithubgithub-pagesgithub-actionsflutter-web

Remove base-href from index.html on build time


I am trying to build a web app using GitHub action. On jobs there is one step

 - run: flutter build web --release --web-renderer html 

This will generate <base href="/"> inside index.html.

When I open the deployment page, it is completely blank.

After spending few days, I found some solutions like removing href from index.html or providing <base href="./">. But this is a manual process, on the other hand build and deployment is handled by GitHub Action.

Flutter added base-href command in web #80519 but the base-href require / on start and end mentioned on commands/build_web.

 if(!(stringArg('base-href').startsWith('/') && stringArg('base-href').endsWith('/')))
    {
       throwToolExit('base-href should start and end with /');
    }

How can I remove or pass --base-href ./ to automate the process.

build_deploy.yaml

name: Flutter Web
on:
  push:
    branches:
      - master
jobs:
  build:
    name: Build Web
    env:
      my_secret: ${{secrets.commit_secret}}
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1
      - uses: subosito/flutter-action@v1
        with:
          channel: 'stable'
      - run: flutter config --enable-web
      - run: flutter clean
      - run: flutter pub get
      # base ref need to remove or change manually(./)
      - run: flutter build web --release --web-renderer html --base-href /./
      - run: |
          cd build/web
          git init
          git config --global user.email [email protected]
          git config --global user.name yeasin50
          git status
          git remote add origin https://${{secrets.commit_secret}}@github.com/yeasin50/flavors_with_flutter.git
          git checkout -b gh-pages
          git add --all
          git commit -m "update"
          git push origin gh-pages -f

On test repository


Solution

  • I have just found a solution, It can be changed from web/index.html on project root directory.

    <base href="$FLUTTER_BASE_HREF">
    

    And on deployment git action it is necessary to provide repository name on base-href like --base-href /repo_name/

    While the repository name is flavors_with_flutter it will be

    - run: flutter build web --release --web-renderer html --base-href /flavors_with_flutter/