Create a website with astrojs

Create a website with astrojs

Feb 15 Β·
3 Min Read

πŸš€ 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:

Terminal window
npm create astro@latest

Alternatively, using pnpm:

Terminal window
pnpm create astro@latest

You will be prompted to:

  1. Give your project a name (e.g., astro-portfolio).
  2. Select a template (choose the one that best fits your needs, like β€œBlog” or β€œMinimal Starter”).
  3. Enable TypeScript (optional but recommended for larger projects).
  4. Install dependencies (choose your preferred package manager).

Navigate into your project directory:

Terminal window
cd astro-portfolio

Step 2: Start the Development Server

To see your new Astro site in action, run:

Terminal window
npm run dev

Or with pnpm:

Terminal window
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 frontmatter
const { 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:

Terminal window
npm install tailwindcss

Initialize Tailwind:

Terminal window
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:

Terminal window
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:

src/components/Counter.jsx
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:

Terminal window
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:

Terminal window
npm install vercel -g
vercel 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


πŸŽ‰ Conclusion

You’ve now set up a full development stack using Astro.js! This stack is highly flexible, enabling you to:

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! πŸš€

Last edited Feb 16