| description | Astro component patterns for pages, layouts, and routing |
|---|---|
| applyTo | **/*.astro |
Astro is used for page routing, layouts, and static content. Svelte components handle interactivity.
---
// Frontmatter: Server-side code (runs at build time)
import Layout from '../layouts/Layout.astro';
import Component from '../components/Component.svelte';
interface Props {
title: string;
description?: string;
}
const { title, description } = Astro.props;
---
<Layout title={title}>
<!-- HTML content -->
<Component client:only="svelte" />
</Layout>- Create reusable layout components in
src/layouts/ - Use
<slot />for content injection - Include common elements:
<head>, navigation, footer - Import global styles in layouts
---
interface Props {
title: string;
}
const { title } = Astro.props;
---
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>{title}</title>
</head>
<body>
<slot />
</body>
</html>- Create pages in
src/pages/ - File-based routing:
src/pages/about.astro→/about - Dynamic routes:
src/pages/game/[id].astro
For dynamic routes, export getStaticPaths():
---
export async function getStaticPaths() {
const games = await fetchGames();
return games.map(game => ({
params: { id: game.id },
props: { game }
}));
}
const { game } = Astro.props;
---
<Layout title={game.title}>
<!-- Game details -->
</Layout>Use client directives to control hydration:
client:only="svelte"- Only runs on client (use for most interactive components)client:load- Hydrates immediately on page loadclient:visible- Hydrates when component becomes visibleclient:idle- Hydrates when browser is idle
---
import GameList from '../components/GameList.svelte';
---
<Layout>
<GameList client:only="svelte" />
</Layout>- Use TypeScript for type-safe props
- Define
Propsinterface in frontmatter - Type component imports
- Keep Astro components for static content and routing
- Use Svelte for interactivity and client-side state
- Minimize client-side JavaScript by using Astro's static rendering where possible
- Import and use global CSS styles from layouts