
- Astro JS Tutorial
- Astro JS - Home
- Astro JS - Overview
- Astro JS vs Next JS
- Astro JS vs React JS
- Astro JS Setup
- Astro JS - Installation
- Astro JS - Project Structure
- Astro JS - Pages
- Astro JS Architecture
- Astro JS - Islands
- Astro JS - Islands Architecture
- Astro JS Routing
- Astro JS - Routing
- Astro JS - Dynamic Routing
- Astro JS - Redirecting Routes
- Astro JS - i18n Routing
- Astro JS Configuration
- Astro JS - Configuration
- Astro JS - Editor Setup
- Astro JS - TypeScript Configuration
- Astro JS - Environment Variables
- Astro JS Build UI
- Astro JS - Components
- Astro JS - Slots
- Astro JS - Layouts
- Astro JS - Fonts
- Astro JS - Scripts
- Astro JS Create Website
- Astro JS - Markdown Contents
- Astro JS - Add Images
- Astro JS - Manage Content
- Astro JS - Content Collections
- Astro JS - Data Fetching
- Astro JS Styling and CSS
- Astro JS - Styling
- Astro JS - CSS Integration
- Astro JS - CSS Cascading Order
- Astro JS Integrations
- Astro JS - React Integrations
- Astro JS - Svelte Integrations
- Astro JS - Solid Integrations
- Astro JS - Vue Integrations
- Astro JS Adapters
- Astro JS - Netlify Adapter
- Astro JS - Cloudflare Adapter
- Astro JS Testing and Deployment
- Astro JS - Testing
- Astro JS - Deployment
- Astro JS Advanced Topics
- Astro JS - State Management
- Astro JS - Prefetching
- Astro JS - Middleware
- Astro JS - Endpoints
- Astro JS - Authentication
- Astro JS - Bun Environment
- Astro JS - Docker
- Astro JS - View Transition
- Astro JS - Transition Directives
- Astro JS - Astro DB
- Astro JS - Bundling
- Astro JS Useful Resources
- Astro JS - Interview Questions
- Astro JS - Cheatsheet
Astro JS - Deployment
Deploy Astro Project
Astro supports deployment to various platforms, such as Vercel, Netlify, AWS, and more. The deployment process may vary depending on the platform you choose. In this section, we will discuss how to deploy an Astro project to some of the popular platforms.
Creating a Production Build
Before deploying a Astro application, you need to create a production build. To create a production build, run the following command:
>> npm run build
Running the above command, generates an optimized version of your application for production. HTML, CSS, and JavaScript files are created based on your pages. JavaScript is compiled and browser bundles are minified using the Astro Compiler to help achieve the best performance and support all modern browsers.
Deploying to Github Pages
GitHub Pages is a static site hosting service that allows you to host your website directly from a GitHub repository. To deploy a Astro application to GitHub Pages, follow these steps:
Step 1: Create a GitHub Repository
Create a github repository with name of your project. For example, testing-app.
In your project, add base path, output, image optimization method to /astro.config.mjs file. The base path is the subdirectory where your site will be hosted. For example, if your GitHub username is astronaut and your repository name is my-repo, the base path would be astronaut.github.io/my-repo.
// astro.config.mjs import { defineConfig } from 'astro/config' export default defineConfig({ site: 'https://astronaut.github.io', base: 'my-repo', })
In the above code, replace astronaut with your GitHub username and my-repo with your repository name.
Step 2: Install the GitHub Pages Action
Now, create a .github/workflows/deploy.yml file in your project root directory with following code. This will automate the deployment process.
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: - name: Checkout your repository using git uses: actions/checkout@v4 - name: Install, build, and upload your site uses: withastro/action@v3 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
Step 3: Configure GitHub Pages
In your GitHub repository settings, Go to Settings > Pages, Under "Build and deployment", select "GitHub Actions" as the source. Make sure GitHub Pages is enabled for your repository.
In the "Source" section, select the branch you want to deploy from (usually main) and the folder (usually /root). Click "Save".
Step 4: Commit and Push Your Changes
Now, push your code to the main branch. GitHub Actions will automatically build and deploy your Astro application to GitHub Pages.
// Add changes to git git add . // Commit changes git commit -m "Configure GitHub Pages deployment" // Push changes git push origin main
After successfully deploying, you can see that a link to access deployed page in deployment section of your repository. We have deployed navigation and linking page that we created in previous chapter. You can access the live page here: farzzn.github.io/testing-app/