DEV Community

Deploy an Astro Project Site to GitHub Pages with Correct Base Paths

If an Astro site works at / locally but breaks when GitHub Pages serves it at /repository-name/, the problem is usually not the components. It is the site root.

GitHub Pages project sites live below a repository path. Astro needs to know that path when it builds links, assets, canonical URLs, and sitemap entries.

This tutorial shows the configuration used by paladini/especialista-ia-automacao-lagoa, an open-source Astro landing page published as a GitHub Pages project site. The result is a static Astro build that can be served at a project URL such as https://username.github.io/repository-name/, while still producing SEO metadata and a sitemap for the deployed URL.

TL;DR Set both site and base in astro.config.mjs, use the base path in internal links, generate a sitemap with @astrojs/sitemap, and deploy the dist directory through GitHub Actions. Then inspect the built HTML and the public sitemap before trusting the deployment.

One important caveat applies to the repository used for this example: its current lockfile is out of sync with package.json, so npm ci currently fails because marked@15.0.12 is missing from the lockfile. The build succeeds after npm install, but the lockfile should be repaired and committed before relying on the workflow in a new checkout.

Prerequisites

You need:

  • Node.js 22 for the repository's current workflow
  • npm
  • A public GitHub repository
  • GitHub Pages enabled with GitHub Actions as the publishing source
  • An Astro project with a build script

Astro's current GitHub Pages guide explains the same project-site constraint: a repository site normally lives below /<repository-name>, so Astro needs a matching base value. See Astro's GitHub Pages deployment guide.

1. Configure the deployed URL and project path

Create or update astro.config.mjs:

import { defineConfig } from 'astro/config';
import sitemap from '@astrojs/sitemap';
export default defineConfig({
  site: 'https://paladini.io',
  base: '/especialista-ia-automacao-lagoa/',
  integrations: [sitemap()],
  build: {
    format: 'file',
  },
});

The site value is the final public origin. Astro uses it for generated sitemap URLs and other absolute URLs. The base value is the path below that origin where the project is served.

For a default GitHub Pages URL, the equivalent configuration would look like this:

export default defineConfig({
  site: 'https://username.github.io',
  base: '/repository-name',
});

The trailing slash style is less important than consistency. Use the form expected by your Astro version and keep the same path in your links and deployment checks.

2. Make internal links base-aware

A configured base does not magically fix strings that begin with /. This link points to the domain root, not the project site:

<a href="/about">About</a>

For a project site, include the repository path:

<a href="/especialista-ia-automacao-lagoa/about">About</a>

The example project uses a single page with fragment links such as #servicos, so those anchors remain local to the generated page. Asset paths and page links still need review.

Search your source for root-relative paths before deploying:

rg -n 'href="/|src="/|url\(/' src public astro.config.mjs

Prefer Astro's URL helpers or carefully constructed paths when the project grows beyond one page. The goal is to make the generated HTML point to the project site, not to / on the host domain.

3. Add sitemap generation

Install the official integration if it is not already present:

npm install @astrojs/sitemap

Then add sitemap() to the integrations array and provide site. The Astro sitemap documentation says the build creates sitemap-index.xml and numbered sitemap files for statically generated routes.

The example also keeps a public/robots.txt file that points crawlers to the project site's sitemap:

User-agent: *
Allow: /
Sitemap: https://paladini.io/especialista-ia-automacao-lagoa/sitemap-index.xml

This is not a ranking guarantee. It simply gives crawlers a stable location for the URLs the build generated.

4. Build locally and inspect the output

Install dependencies and build the static output:

npm install
npm run build

Astro writes the result to dist. Check that the expected files exist and that the project path appears in generated HTML and XML:

rg -n "especialista-ia-automacao-lagoa|sitemap-index|canonical" dist

In the current checkout, the build produced one HTML page, a sitemap index, a numbered sitemap, and robots.txt. The generated HTML contained a canonical URL under /especialista-ia-automacao-lagoa/, and the sitemap index was generated under the same project path.

This is also where the lockfile problem becomes visible. The repository's workflow uses npm ci, but a clean checkout currently reports that marked@15.0.12 is missing from package-lock.json. npm install can resolve the dependency locally and the build completes, but that does not repair CI. Regenerate and review the lockfile in the repository before treating the workflow as reliable.

5. Deploy dist with GitHub Actions

The repository uses two jobs: one builds and uploads the artifact, and the other deploys it:

name: Deploy to GitHub Pages
on:
  push:
    branches: [main]
  workflow_dispatch:
permissions:
  contents: read
  pages: write
  id-token: write
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 22
          cache: npm
      - run: npm ci
      - run: npm run build
      - uses: actions/upload-pages-artifact@v3
        with:
          path: dist
  deploy:
    needs: build
    runs-on: ubuntu-latest
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    steps:
      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@v4

The needs: build dependency matters. It prevents the deploy job from racing ahead before an artifact exists. The pages: write and id-token: write permissions are also part of GitHub's documented requirements for the Pages deployment action. See GitHub's custom workflow documentation.

Before enabling the workflow, go to repository Settings, open Pages, and select GitHub Actions as the source. Then repair the lockfile and confirm that a clean npm ci succeeds.

Verify the deployed site

After a successful deployment, check the actual URLs instead of relying only on a green workflow:

curl -I https://username.github.io/repository-name/
curl -I https://username.github.io/repository-name/sitemap-index.xml
curl -s https://username.github.io/repository-name/ | grep -o 'canonical[^>]*'

You want an HTTP success response, a sitemap that is reachable below the project path, and a canonical URL that points to the deployed site. Also click every navigation link and inspect images and styles in a browser. A page can return 200 while still requesting assets from the wrong root.

The live example is reachable at paladini.io/especialista-ia-automacao-lagoa/, with its sitemap index and robots.txt returning successfully.

Why this works

site answers "what is the deployed origin?" base answers "what path is this project mounted under?" The sitemap integration uses the site configuration to create absolute URLs, while the build uses the base configuration when resolving the project root. GitHub Pages then serves the generated dist directory as a static artifact.

If any one of those layers disagrees, the failure is predictable: broken assets, incorrect canonical URLs, sitemap entries at the wrong location, or a page that works only at localhost.

Common failure modes

The homepage loads but CSS is missing. Inspect asset URLs in the generated HTML. A root-relative /assets/... request is likely bypassing the project path.

The sitemap is generated at the wrong URL. Check that site is the final public origin and that the sitemap integration is enabled. For a project site, verify both the origin and repository path.

CI fails at npm ci. Do not replace the command silently with npm install in production CI. npm ci is intentionally strict. Repair and commit the lockfile, then rerun the clean install. In the example repository, this is a known current limitation.

A custom domain changes the path assumption. If the site moves from a project URL to a custom domain at the root, revisit base, internal links, canonical URLs, and the sitemap. The correct configuration for one hosting shape can be wrong for another.

FAQ

Do I need base for username.github.io? Usually not when the repository itself is named username.github.io, because the site is served at the domain root. A repository project site normally does need it.

Does a sitemap improve rankings automatically? No. It helps crawlers discover generated URLs, but it does not guarantee indexing, rankings, or traffic.

Is the project ready for production? The static build and public site are verifiable, but the current lockfile mismatch means the repository's clean CI path needs repair before making that claim.

Takeaway

For Astro on GitHub Pages, deployment correctness starts with URL correctness. Set site and base, make links aware of the project path, generate and inspect the sitemap, and test the exact public URLs. Treat a strict npm ci failure as a repository maintenance issue, not as a reason to hide a broken lockfile.

What is the first generated URL you inspect when a project-site deployment looks correct locally but breaks after publishing?

AI assistance disclosure: I used AI assistance to organize and edit this tutorial. The repository configuration, build output, public URLs, and cited documentation were checked against the sources listed above.

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.