What is astro?
Astro is a modern static site builder that allows you to build fast, optimized websites using your favorite tools and frameworks. It focuses on delivering a zero-JavaScript runtime by default, making it ideal for performance-focused web projects.
What are astro's main functionalities?
Static Site Generation
Astro allows you to generate static sites with ease. The configuration file specifies the site URL and output directory for the build.
```javascript
// astro.config.mjs
import { defineConfig } from 'astro/config';
export default defineConfig({
site: 'https://example.com',
build: {
outDir: 'dist',
},
});
```
Component Framework Agnostic
Astro supports multiple component frameworks like React, Vue, Svelte, and more. You can use your preferred framework within Astro components.
```jsx
// src/pages/index.astro
---
import React from 'react';
import MyComponent from '../components/MyComponent.jsx';
---
<MyComponent />
```
Markdown Support
Astro has built-in support for Markdown, making it easy to write content for your static site.
```markdown
---
title: 'My Blog Post'
date: '2023-10-01'
---
# Hello, World!
This is a blog post written in Markdown.
```
Partial Hydration
Astro's partial hydration feature allows you to load JavaScript only when necessary, improving performance by reducing the amount of JavaScript sent to the client.
```jsx
// src/components/InteractiveComponent.jsx
import { useState } from 'react';
export default function InteractiveComponent() {
const [count, setCount] = useState(0);
return (
<button onClick={() => setCount(count + 1)}>
Count: {count}
</button>
);
}
// src/pages/index.astro
---
import InteractiveComponent from '../components/InteractiveComponent.jsx';
---
<InteractiveComponent client:load />
```
Other packages similar to astro
next
Next.js is a popular React framework for building server-side rendered and static web applications. It offers a rich set of features like API routes, dynamic routing, and built-in CSS support. Compared to Astro, Next.js is more focused on server-side rendering and full-stack capabilities.
gatsby
Gatsby is a React-based framework for building static sites and apps. It emphasizes performance and scalability, offering a rich plugin ecosystem and GraphQL data layer. While Gatsby is similar to Astro in terms of static site generation, it is more tightly coupled with React and has a steeper learning curve.
eleventy
Eleventy is a simpler static site generator that focuses on flexibility and minimalism. It supports multiple templating languages and is highly configurable. Unlike Astro, Eleventy does not have built-in support for modern JavaScript frameworks like React or Vue.
Astro is a fresh but familiar approach to building websites. Astro combines decades of proven performance best practices with the DX improvements of the component-oriented era.
With Astro, you can use your favorite JavaScript framework and automatically ship the bare-minimum amount of JavaScriptβby default, it's none at all!
π§ Setup
npm init astro@shhhhh ./my-astro-project
cd ./my-astro-project
npm install
npm start
π Build & Deployment
The default Astro project has the following scripts
in the /package.json
file:
{
"scripts": {
"start": "astro dev .",
"build": "astro build ."
}
}
For local development, run:
npm run start
To build for production, run the following command:
npm run build
To deploy your Astro site to production, upload the contents of /dist
to your favorite static site host.
π₯Ύ Guides
π Basic Usage
Even though nearly-everything is configurable, we recommend starting out by creating an src/
folder in your project with the following structure:
βββ src/
β βββ components/
β βββ pages/
β βββ index.astro
βββ public/
βββ package.json
src/components/*
: where your reusable components go. You can place these anywhere, but we recommend a single folder to keep them organized.src/pages/*
: this is a special folder where your routing lives.
π¦ Routing
Routing happens in src/pages/*
. Every .astro
or .md.astro
file in this folder corresponds with a public URL. For example:
Local file | Public URL |
---|
src/pages/index.astro | /index.html |
src/pages/post/my-blog-post.md.astro | /post/my-blog-post/index.html |
π Static Assets
Static assets should be placed in a public/
folder in your project. You can place any images, fonts, files, or global CSS in here you need to reference.
πͺ¨ Generating HTML with Astro
TODO: Astro syntax guide
β‘ Dynamic Components
TODO: Astro dynamic components guide
π§ Partial Hydration
By default, Astro outputs zero client-side JS. If you'd like to include an interactive component in the client output, you may use any of the following techniques.
<MyComponent />
will render an HTML-only version of MyComponent
(default)<MyComponent:load />
will render MyComponent
on page load<MyComponent:idle />
will use requestIdleCallback() to render MyComponent
as soon as main thread is free<MyComponent:visible />
will use an IntersectionObserver to render MyComponent
when the element enters the viewport
βοΈ State Management
Frontend state management depends on your framework of choice. Below is a list of popular frontend state management libraries, and their current support with Astro.
Our goal is to support all popular state management libraries, as long as there is no technical reason that we cannot.
Are we missing your favorite state management library? Add it to the list above in a PR (or create an issue)!
π
Styling
Styling in Astro is meant to be as flexible as youβd like it to be! The following options are all supported:
Framework | Global CSS | Scoped CSS | CSS Modules |
---|
Astro (.astro ) | β
| β
| N/AΒΉ |
React / Preact | β
| β | β
|
Vue | β
| β
| β
|
Svelte | β
| β
| β |
ΒΉ .astro
files have no runtime, therefore Scoped CSS takes the place of CSS Modules (styles are still scoped to components, but donβt need dynamic values)
To learn more about writing styles in Astro, see our Styling Guide.
π Styling
πΆ Fetching Data
Fetching data is what Astro is all about! Whether your data lives remotely in an API or in your local project, Astro has got you covered.
For fetching from a remote API, use a native JavaScript fetch()
(docs) as you are used to. For fetching local content, use Astro.fetchContent()
(docs).
---
const remoteData = await fetch('https://api.mysite.com/v1/people').then((res) => res.json());
const localData = Astro.fetchContent('../post/*.md');
---
πΊοΈ Sitemap
Astro will automatically create a /sitemap.xml
for you for SEO! Be sure to set buildOptions.site
in your Astro config so the URLs can be generated properly.
β οΈ Note that Astro wonβt inject this into your HTML for you! Youβll have to add the tag yourself in your <head>
on all pages that need it:
<link rel="sitemap" href="/sitemap.xml" />
Examples
π± Collections (beta)
Fetching data is easy in Astro. But what if you wanted to make a paginated blog? What if you wanted an easy way to sort data, or filter data based on part of the URL? Or generate an RSS 2.0 feed? When you need something a little more powerful than simple data fetching, Astroβs Collections API may be what you need.
π Collections API
βοΈ Config
π astro.config.mjs
Reference
π API
π Full API Reference
π©π½βπ» CLI
π Command Line Docs
π Development Server
π Dev Server Docs