Skip to content

Astro.js

  1. Create a folder. Drag it into VS Code to open the project.
  2. In the VS Code terminal run
Terminal window
npm create astro@latest
  1. Follow the wizard:
    1. Where should we create your new project? ./
    2. How would you like to start your new project? Use minimal (empty) template
    3. Install dependencies? Yes
    4. Initialize a new git repository? Yes
  2. Run npm run dev to start the dev server
  3. Open the localhost URL (port 4321 if available) that is output to view your site.

Astro files can be used to:

  1. Create files for your website: index.astro
  2. Create components that can be reused throughout your website.
  3. Create layout files that can insert other content into <slots />

Astro components are the basic building blocks of any Astro project.

HelloComponent.astro
---
const props = Astro.props;
---
<h1>Hello, World!</h1>

To use it simply make the above file and import it in another file:

index.astro
import HelloComponent from "./HelloComponent.astro";
<HelloComponent />

Inside a .astro file:

  1. Javascript in the frontmatter is executed at build time to create static HTML for your site, and then the code is “thrown away.”
  2. 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>

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

https://docs.astro.build/en/concepts/islands/#client-islands

  1. Correctly define config properties for publishing and update base URLs. ✏️ Add the full URL to your site
  2. Deploy your Astro site using Github Pages (follow How to deploy but use the action on this page).

The below configuration example fixes your base and site properties to publish on Github.

  1. Open astro.config.mjs
  2. In defineConfig({}), change site to https:// + your username + .github.io.
  3. Change base to / + your repository name.
  4. Add output: 'static',.
  5. Compare your file to the one below published at https://omundy.github.io/tarheeltrailblazers-astro
// @ts-check
import { 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()]
});

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.

  1. Open the following files and add const base = import.meta.env.BASE_URL; to end of the frontmatter in each file.
src/components/BlogPost.astro
src/components/Navigation.astro
src/layouts/MarkdownPostLayout.astro
src/pages/tags/index.astro
  1. In src/components/BlogPost.astro change the link in the body:
<a href={`${base}${url}`}>{title}</a>
  1. In src/components/Navigation.astro change 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>
  1. In src/layouts/MarkdownPostLayout.astro change the tag link in the body:
<a href={`${base}/tags/${tag}`}>{tag}</a>
  1. In src/pages/tags/index.astro change the tag link in the body:
<a href={`${base}/tags/${tag}`}>{tag}</a>

Astro supports several types of integrations to import components in various languages.

Follow steps below to add a svelte integration

  1. The astro add command installs Svelte and updates your astro.config.mjs file to include the Svelte integration.
npx astro add svelte
  1. Create a Svelte Component

src/components/IdeaGenerator.svelte

  1. 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>
  1. Import the component
import Counter from '@/components/Counter.svelte';
<Counter client:load />

You should see something like this:

  1. Here is another demo component. Find it in the components/examples directory of this project and publish it.
  1. See the example to use fetch() with an API and Svelte’s params to pass data to the component.
ThemeTailwindBlogMarkdownSimplicityThemes🌟Experience
AstroWind (demo)⚠️5.4k⚠️
Accessible Astro Starter (demo)1.1kcamplajolla.org
Titan Core (demo)⚠️74⚠️
Bloomfolio (demo)61n/a
Saral Theme (demo)53n/a
Astro-Bootstrap (demo)40n/a
Astro Base (demo)⚠️13⚠️

Allow non-technical editors to manage content on your Astro site. Some notable CMSs from the large list of possible integrations.

Terminal window
# Upgrade Astro and official integrations together
npx @astrojs/upgrade

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

index.mdx
---
---
import Component from '@/components/Component.astro';
<Component>
<p>Hello, in red!</p>
</Component>
Component.astro
<div>
<slot />
</div>
<style is:global>
p { background-color: red; }
</style>

See Install Tailwind CSS with Astro

Aliases can help improve the development experience in codebases with many directories or relative imports.

  1. Open tsconfig.json located at the root of your project. Astro projects include a tsconfig.json file by default.
  2. Add baseUrl and paths to the compilerOptions object. The baseUrl should be set to . (the project root), and the paths define your aliases. A common setup uses @/ to point to the /src/ directory.
  3. 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';
---