By - Azaz | Published - 08 Nov 2024 | Updated - 08 Nov 2024 | Go to - Comments & Questions
Astro is a modern, lightweight, and versatile framework for building fast, content-focused websites. Known for its "Islands Architecture," Astro only loads JavaScript where it's needed, making it ideal for static sites and content-rich applications that prioritize performance.
If you're ready to create your first Astro app, this guide will walk you through the steps to set up, build, and deploy a basic Astro application.
1 node -v
1 npm create astro@latest
1 cd your-project-name2 npm install
Astro projects follow a specific structure:
This folder structure keeps everything organized and makes it easy to scale the project.
1 ---2 // Frontmatter, if needed, goes here (e.g., data imports)3 ---4 5 <html>6 <head>7 <title>My First Astro App</title>8 </head>9 <body>10 <h1>Welcome to My First Astro App!</h1>11 <p>This is a simple Astro page.</p>12 </body>13 </html>
1 npm run dev
1 ---2 // Using slots for injecting page-specific content3 const { title } = Astro.props;4 ---5 6 <html>7 <head>8 <title>{title}</title>9 </head>10 <body>11 <header>12 <h1>My Astro App</h1>13 </header>14 <main>15 <slot />16 </main>17 <footer>18 <p>© 2024 My Astro App</p>19 </footer>20 </body>21 </html>
1 ---2 import BaseLayout from '../layouts/BaseLayout.astro';3 ---4 5 <BaseLayout title="Home">6 <h2>Welcome to My First Astro App!</h2>7 <p>This is a simple Astro page with a layout.</p>8 </BaseLayout>
1 npm install @astrojs/react react react-dom
1 import { defineConfig } from 'astro/config';2 import react from '@astrojs/react';3 4 export default defineConfig({5 integrations: [react()],6 });
1 import { useState } from 'react';2 3 const Counter = () => {4 const [count, setCount] = useState(0);5 6 return (7 <button onClick={() => setCount(count + 1)}>8 Count: {count}9 </button>10 );11 };12 13 export default Counter;
1 ---2 import BaseLayout from '../layouts/BaseLayout.astro';3 import Counter from '../components/Counter.jsx';4 ---5 6 <BaseLayout title="Home">7 <h2>Welcome to My First Astro App!</h2>8 <p>This is a simple Astro page with a layout and a React component:</p>9 <Counter client:load />10 </BaseLayout>
1 npm run build
1 npm run preview
1 npm install -g vercel2 vercel
Congratulations! You've just built your first Astro app. With its unique Islands Architecture, Astro is a fantastic choice for creating fast, content-focused websites with minimal JavaScript. From adding layouts to integrating with React, Astro offers flexibility and performance.
Enjoy building with Astro, and feel free to explore more of its powerful features as you dive deeper!
We are a passionate team of developers and designers dedicated to helping businesses thrive in the digital age. Through cutting-edge technologies and innovative solutions, we empower you to achieve success with powerful websites, custom software, and intelligent digital products.