Create a website with astrojs
π Setting Up a Development Stack with Astro.js
Prerequisites
Make sure you have the following installed on your system:
Step 1: Install Astro
Open your terminal and run the following command to create a new Astro project:
npm create astro@latest
Alternatively, using pnpm:
pnpm create astro@latest
You will be prompted to:
- Give your project a name (e.g.,
astro-portfolio
). - Select a template (choose the one that best fits your needs, like βBlogβ or βMinimal Starterβ).
- Enable TypeScript (optional but recommended for larger projects).
- Install dependencies (choose your preferred package manager).
Navigate into your project directory:
cd astro-portfolio
Step 2: Start the Development Server
To see your new Astro site in action, run:
npm run dev
Or with pnpm:
pnpm dev
Open your browser and go to http://localhost:3000
to see your site live. Any changes you make will automatically update in the browser.
Step 3: Project Structure
Astroβs file structure is simple yet powerful:
astro-portfolio/βββ public/ # Static assets (images, fonts, etc.)βββ src/β βββ components/ # Reusable UI componentsβ βββ layouts/ # Page layoutsβ βββ pages/ # Website pages (.astro or .md files)βββ astro.config.mjs # Astro configurationβββ package.json # Project dependencies and scripts
Step 4: Adding Pages
To create a new page, navigate to the src/pages/
directory and create a new file:
Example: src/pages/about.astro
---title: "About Me"---
<h1>About Me</h1><p>Hello! I'm a web developer passionate about clean code and fast websites.</p>
This automatically becomes available at http://localhost:3000/about
.
Step 5: Creating Components
Organize reusable UI elements in src/components/
.
Example: src/components/Button.astro
---// Props can be passed in using Astro's frontmatterconst { text, link } = Astro.props;---
<a href={link} class="button">{text}</a>
<style> .button { background-color: #0070f3; color: white; padding: 10px 20px; border-radius: 5px; text-decoration: none; }</style>
Use this component in a page:
---import Button from "../components/Button.astro";---
<h1>Welcome to My Site</h1><Button text="Learn More" link="/about" />
Step 6: Styling with Tailwind CSS
Astro supports Tailwind CSS out of the box. Install it with:
npm install tailwindcss
Initialize Tailwind:
npx tailwindcss init
Configure Tailwind in tailwind.config.cjs
:
module.exports = { content: ["./src/**/*.{astro,html,js,jsx,ts,tsx}"], theme: { extend: {}, }, plugins: [],};
Add Tailwind directives to src/styles/global.css
:
@tailwind base;@tailwind components;@tailwind utilities;
Import this in your layout or root component:
<link rel="stylesheet" href="/src/styles/global.css">
You can now use Tailwind classes in your Astro components:
<h1 class="text-4xl font-bold text-center">Hello, Astro!</h1>
Step 7: Adding Integrations
Astro supports integrations to extend functionality, such as React, Vue, or Markdown support.
Example: Install React integration:
npm install @astrojs/react
Add it to your astro.config.mjs
:
import { defineConfig } from 'astro/config';import react from '@astrojs/react';
export default defineConfig({ integrations: [react()],});
Now, you can use React components inside Astro:
import { useState } from 'react';
export default function Counter() { const [count, setCount] = useState(0); return ( <button onClick={() => setCount(count + 1)}> Count: {count} </button> );}
And use it in your Astro page:
---import Counter from "../components/Counter.jsx";---
<Counter client:load />
The client:load
directive ensures the React component is only loaded on the client side, keeping your initial HTML lightweight and fast.
Step 8: Building and Deploying
Build your site for production with:
npm run build
The static assets are generated in the dist
folder. Deploy this folder to any static hosting service like Vercel, Netlify, or DigitalOcean.
For Vercel deployment:
npm install vercel -gvercel deploy
For DigitalOcean, you can push to a GitHub repository and connect it to an App Platform project for continuous deployment.
Step 9: Enhancements and Best Practices
- SEO: Use Astroβs built-in support for meta tags and Open Graph tags.
- Accessibility: Ensure accessible navigation and semantic HTML.
- Performance: Astro is fast by default, but you can further optimize images and critical CSS.
- Analytics: Integrate Google Analytics or other services with ease using script tags in the head section.
π Conclusion
Youβve now set up a full development stack using Astro.js! This stack is highly flexible, enabling you to:
- Build static sites with incredible speed and SEO benefits.
- Integrate modern UI frameworks like React or Vue when needed.
- Maintain clean, maintainable, and scalable code.
Feel free to modify and expand on this setup to suit your projectβs needs. If you need help with deployment or integrating additional features, let me know! Happy coding! π