Astro.js
Create an Astro project
Section titled “Create an Astro project”- Create a folder. Drag it into VS Code to open the project.
- In the VS Code terminal run
npm create astro@latest- Follow the wizard:
- Where should we create your new project?
./ - How would you like to start your new project?
Use minimal (empty) template - Install dependencies?
Yes - Initialize a new git repository?
Yes
- Where should we create your new project?
- Run
npm run devto start the dev server - Open the localhost URL (port
4321if available) that is output to view your site.
Astro Files
Section titled “Astro Files”Astro files can be used to:
- Create files for your website:
index.astro - Create components that can be reused throughout your website.
- Create layout files that can insert other content into
<slots />
Astro Components
Section titled “Astro Components”Astro components are the basic building blocks of any Astro project.
---const props = Astro.props;---<h1>Hello, World!</h1>To use it simply make the above file and import it in another file:
import HelloComponent from "./HelloComponent.astro";
<HelloComponent />Javascript in Astro
Section titled “Javascript in Astro”Inside a .astro file:
- Javascript in the frontmatter is executed at build time to create static HTML for your site, and then the code is “thrown away.”
- JS in a
<script>tag only runs in the browser.
---import Header from "../components/Header.astro";import Footer from "../components/Footer.astro";import "../styles/global.css";const pageTitle = "Home";---
<html lang="en"><head> <title>{pageTitle}</title></head><body> <Header /> <h1>{pageTitle}</h1> <Footer /> <script> import "../scripts/menu.js"; console.log("hello from the DevTools console!"); </script></body></html>Astro routing
Section titled “Astro routing”A function to generate multiple, prerendered page routes from a single .astro page component with one or more parameters in its file path. Use this for routes that will be created at build time, also known as static site building. https://docs.astro.build/en/reference/routing-reference/#getstaticpaths
Astro Islands
Section titled “Astro Islands”https://docs.astro.build/en/concepts/islands/#client-islands
Astro Tutorials
Section titled “Astro Tutorials”- Follow Astro’s Build a Blog tutorial to create a modular website using component-based design.
Publish Astro Site on Github Pages.
Section titled “Publish Astro Site on Github Pages.”- Correctly define config properties for publishing and update base URLs. ✏️ Add the full URL to your site
- Deploy your Astro site using Github Pages (follow How to deploy but use the action on this page).
Define config properties for Github Pages
Section titled “Define config properties for Github Pages”The below configuration example fixes your base and site properties to publish on Github.
- Open
astro.config.mjs - In
defineConfig({}), changesitetohttps://+ your username +.github.io. - Change
baseto/+ your repository name. - Add
output: 'static',. - Compare your file to the one below published at https://omundy.github.io/tarheeltrailblazers-astro
// @ts-checkimport { defineConfig } from 'astro/config';import svelte from "@astrojs/svelte";
export default defineConfig({ site: 'https://omundy.github.io', // DOMAIN base: '/tarheeltrailblazers-astro', // REPOSITORY output: 'static', integrations: [svelte()]});Update base urls for Github Pages
Section titled “Update base urls for Github Pages”99% of websites at Github Pages are published inside folders in a subdomain. For example, the domain for the site you are view ing is https://omundy.github.io while the folder is /dig345-radical-software. Unless you are publish directly to a domain (the repo name is your username, e.g. omundy.github.io), you need to change the internal links in your site so it can be published at Github Pages.
- Open the following files and add
const base = import.meta.env.BASE_URL;to end of the frontmatter in each file.
src/components/BlogPost.astrosrc/components/Navigation.astrosrc/layouts/MarkdownPostLayout.astrosrc/pages/tags/index.astro- In
src/components/BlogPost.astrochange the link in the body:
<a href={`${base}${url}`}>{title}</a>- In
src/components/Navigation.astrochange the links in the body:
<div id="main-menu" class="nav-links"> <a href={`${base}/`}>Home</a> <a href={`${base}/about`}>About</a> <a href={`${base}/blog`}>Blog</a> <a href={`${base}/tags`}>Tags</a></div>- In
src/layouts/MarkdownPostLayout.astrochange the tag link in the body:
<a href={`${base}/tags/${tag}`}>{tag}</a>- In
src/pages/tags/index.astrochange the tag link in the body:
<a href={`${base}/tags/${tag}`}>{tag}</a>Astro + Svelte
Section titled “Astro + Svelte”Astro supports several types of integrations to import components in various languages.
Add Svelte Integration
Section titled “Add Svelte Integration”Follow steps below to add a svelte integration
- The
astro addcommand installs Svelte and updates yourastro.config.mjsfile to include the Svelte integration.
npx astro add svelte- Create a Svelte Component
src/components/IdeaGenerator.svelte
- Add the follow code
<script> let count = 0; function increment() { count++; }</script>
<button on:click={increment}> Count {count}</button>
<style> button { padding: 0.5em 1em; border-radius: 8px; border: 1px solid #ccc; background-color: #f0f0f0; cursor: pointer; }</style>- Import the component
import Counter from '@/components/Counter.svelte';
<Counter client:load />You should see something like this:
- Here is another demo component. Find it in the
components/examplesdirectory of this project and publish it.
- See the example to use
fetch()with an API and Svelte’s params to pass data to the component.
Astro Resources
Section titled “Astro Resources”- Astro documentation https://docs.astro.build/
Astro Themes
Section titled “Astro Themes”- astro.build/themes - Big list, sortable by free, etc.
- Tailwind themes that use Astro
- Also see Astro Showcase and astro.new with frameworks, integrations, templates, etc.
| Theme | Tailwind | Blog | Markdown | Simplicity | Themes | 🌟 | Experience |
|---|---|---|---|---|---|---|---|
| AstroWind (demo) | ✅ | ✅ | ✅ | ⚠️ | ❌ | 5.4k | ⚠️ |
| Accessible Astro Starter (demo) | ✅ | ✅ | ✅ | ✅ | ❌ | 1.1k | camplajolla.org |
| Titan Core (demo) | ✅ | ✅ | ✅ | ⚠️ | ✅ | 74 | ⚠️ |
| Bloomfolio (demo) | ✅ | ✅ | ✅ | ✅ | ✅ | 61 | n/a |
| Saral Theme (demo) | ✅ | ✅ | ✅ | ✅ | ❌ | 53 | n/a |
| Astro-Bootstrap (demo) | ❌ | ✅ | ✅ | ✅ | ❌ | 40 | n/a |
| Astro Base (demo) | ✅ | ⚠️ | ✅ | ✅ | ❌ | 13 | ⚠️ |
Content Management Systems (CMS)
Section titled “Content Management Systems (CMS)”Allow non-technical editors to manage content on your Astro site. Some notable CMSs from the large list of possible integrations.
Astro Technical Notes
Section titled “Astro Technical Notes”Typescript
Section titled “Typescript”- Add
// @ts-nocheckto the top of the<script>tag (the client side script) to silence all the Typescript errors in your.astrofiles. It saves from adding// @ts-ignoreto each line.
Upgrade Astro
Section titled “Upgrade Astro”# Upgrade Astro and official integrations togethernpx @astrojs/upgradeStyling Slotted Content
Section titled “Styling Slotted Content”Astro CSS rules are automatically scoped by default. Scoped styles are compiled behind-the-scenes to only apply to HTML written inside of that same component.
To make CSS work for content inside a slot, use Astro’s is:global
------import Component from '@/components/Component.astro';<Component><p>Hello, in red!</p></Component><div><slot /></div>
<style is:global>p { background-color: red; }</style>Add Tailwind to Astro
Section titled “Add Tailwind to Astro”See Install Tailwind CSS with Astro
Configure @ Aliases
Section titled “Configure @ Aliases”Aliases can help improve the development experience in codebases with many directories or relative imports.
- Open
tsconfig.jsonlocated at the root of your project. Astro projects include atsconfig.jsonfile by default. - Add
baseUrlandpathsto thecompilerOptionsobject. ThebaseUrlshould be set to.(the project root), and thepathsdefine your aliases. A common setup uses@/to point to the/src/directory. - Restart your development server to ensure the changes are picked up.
{ "extends": "astro/tsconfigs/strict", "include": [".astro/types.d.ts", "**/*"], "exclude": ["dist"], "compilerOptions": { "baseUrl": ".", "paths": { "@/components/*": ["src/components/*"], "@/layouts/*": ["src/layouts/*"], "@/assets/*": ["src/assets/*"], "@/styles/*": ["src/styles/*"], "@/*": ["src/*"] } }}The above changes this
---import Button from '../../components/controls/Button.astro';---To this:
---import Button from '@/components/controls/Button.astro';---