Teacher: Coding In Public Project repo: https://github.com/coding-in-public/astro-blog-tutorial/ Project video tutorial playlist: https://www.youtube.com/watch?v=F2pw1C9eKXw&list=PLoqZcxvpWzzeRwF8TEpXHtO7KYY6cNJeF Framework documentation: https://astro.build/
npm create astro@latest
npm run dev
Fetching data example
---
const res = await fetch('https://jsonplaceholder.typicode.com/posts?_limit=5')
const data = await res.json()
---
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
<meta name="viewport" content="width=device-width" />
<meta name="generator" content={Astro.generator} />
<title>Astro</title>
</head>
<body>
{
data.map((item) => (
<li>{item.title}</li>
))
}
</body>
</html>MainLayout example
---
// Component imports
import MainHead from './MainHead.astro'
interface Props {
title?: string,
description?: string
}
const { title = 'My Astro Blog', description = 'My Astro Description' } = Astro.props as Props
---
<html lang="en">
<MainHead {title} {description} />
<body>
<slot>
</slot>
</body>
</html><p style="font-size: 4rem">Styles in AstroJS</p>
<p
style={{
backgroundColor: 'red',
}}
>
CSS in JS style
</p><MainLayout title="Home" description='My Home Page'>
<p>Testing styles</p>
</MainLayout>
<style>
p {
font-size: 4rem;
}
</style><MainLayout title="Home" description='My Home Page'>
<p>Testing styles</p>
</MainLayout>
<style is:global>
p {
font-size: 4rem;
}
</style>npm i -D sass
<MainLayout title="Home" description='My Home Page'>
<p>Testing <span>styles</span></p>
</MainLayout>
<style lang="scss">
p {
span {
color: blue;
}
}
</style>---
const foregroundColor = "hsl(20, 50%, 50%)"
const backgroundColor = "hsl(200, 50%, 50%)"
---
<MainLayout title="Home" description='My Home Page'>
<p>Testing styles</p>
</MainLayout>
<style define:vars={{ foregroundColor, backgroundColor }}>
p {
background-color: var(--backgroundColor);
color: var(--foregroundColor);
}
</style>---
const isRed = true
// const isRed = false
---
<MainLayout title="Home" description='My Home Page'>
<p class:list={["big"], { red: isRed }}>Testing styles</p>
</MainLayout>
<style>
.big {
font-size: 4rem;
}
.red {
color: red;
}
</style>https://postcss.org/
https://preset-env.cssdb.org/
npm i postcss
npm i postcss-preset-env
https://github.com/natemoo-re/astro-icon#readme
npm i astro-icon
const { frontmatter } = Astro.props
console.log(frontmatter)<p>{frontmatter.title}</p>
<pre>{JSON.stringify(frontmatter, null, 2)}</pre>Kick it into lightspeed with integrations
https://astro.build/integrations/
Schema Markup Generator (JSON-LD)
https://technicalseo.com/tools/schema-markup-generator
What are rich results? Rich results are experiences on Google surfaces, such as Search, that go beyond the standard blue link. Rich results can include carousels, images, or other non-textual elements. https://search.google.com/test/rich-results
Render stuff as HTML
const paragraph = '<p>Hello</p>'
<Fragment set:html={paragraph}>
<div set:html={paragraph} />npm create astro@latest -- --template minimalπ§βπ Seasoned astronaut? Delete this file. Have fun!
Inside of your Astro project, you'll see the following folders and files:
/
βββ public/
βββ src/
β βββ pages/
β βββ index.astro
βββ package.json
Astro looks for .astro or .md files in the src/pages/ directory. Each page is exposed as a route based on its file name.
There's nothing special about src/components/, but that's where we like to put any Astro/React/Vue/Svelte/Preact components.
Any static assets, like images, can be placed in the public/ directory.
All commands are run from the root of the project, from a terminal:
| Command | Action |
|---|---|
npm install |
Installs dependencies |
npm run dev |
Starts local dev server at localhost:4321 |
npm run build |
Build your production site to ./dist/ |
npm run preview |
Preview your build locally, before deploying |
npm run astro ... |
Run CLI commands like astro add, astro check |
npm run astro -- --help |
Get help using the Astro CLI |
Feel free to check our documentation or jump into our Discord server.