---
description: Comprehensive best practices and coding standards for Astro projects, including code organization, performance considerations, security, and testing.
globs: **/*.astro
---
- # Code Organization and Structure
- ## Directory Structure Best Practices
- `src/`: This is the heart of your Astro project.
- `components/`: Reusable UI elements. Organize components into subdirectories based on functionality or feature.
- `layouts/`: Components that define the overall page structure. Use a `BaseLayout.astro` to provide common elements like `
` and ``.
- `pages/`: Special components that create routes. Each file here becomes a page. Use dynamic routes (`[param].astro`) for data-driven content.
- `content/`: (Required for content collections) Store content collections and an optional `config.ts` or `config.js` file.
- `styles/`: Global CSS or Sass files. Use Astro's CSS Modules for component-specific styles.
- `scripts/`: JavaScript modules or utilities. Consider using TypeScript for type safety.
- `utils/`: Helper functions and utilities.
- `types/`: TypeScript type definitions for your project.
- `public/`: Static assets like images, fonts, and favicons. These are copied directly to the `dist/` folder without processing.
- `dist/`: (Generated) The output directory containing the built website. Usually ignored in version control.
- `node_modules/`: (Generated) Project dependencies managed by npm, pnpm or yarn. Usually ignored in version control.
- `.astro/`: (Generated) Internal Astro files. Should not be manually modified.
- ## File Naming Conventions
- **Components:** `ComponentName.astro` (PascalCase).
- **Pages:** `page-name.astro` (kebab-case). Index pages should be named `index.astro`.
- **Layouts:** `LayoutName.astro` (PascalCase).
- **Styles:** `Component.module.css` (component-specific), `global.css` (global styles).
- **Scripts:** `script-name.js` or `script-name.ts` (kebab-case).
- ## Module Organization
- Favor modularity and reusability.
- Group related components and functions into modules within the appropriate directory (e.g., `components/ui/Button.astro`).
- Use clear and descriptive names for files and folders.
- Expose only the necessary parts of a module using `export` and `import` statements.
- ## Component Architecture Recommendations
- **Atomic Design:** Consider using Atomic Design principles to create a scalable and maintainable component library (Atoms, Molecules, Organisms, Templates, Pages).
- **Composition:** Favor composition over inheritance.
- **Slots:** Utilize Astro's slots to create flexible components that accept dynamic content.
- **Props:** Use TypeScript to define prop types for your components.
- **Keep Components Focused:** A component should ideally handle a single responsibility.
- ## Code Splitting Strategies
- **Automatic Code Splitting:** Astro automatically code-splits JavaScript and CSS, so generally you don't need to manually handle code splitting.
- **Dynamic Imports:** Use dynamic imports (`import('./Component.astro')`) for components or modules that are not immediately needed. This is useful for infrequently used components or large libraries.
- **View Transitions:** Astro's View Transitions API also helps with perceived performance, providing smoother navigation between pages instead of full reloads. Optimize assets for transitions by using the `` component.
- # Common Patterns and Anti-patterns
- ## Design Patterns
- **Container/Presentational:** Separate data fetching and logic (container components) from UI rendering (presentational components).
- **Higher-Order Components (HOCs):** Use HOCs or render props (though less common in Astro) to share logic between components.
- **Provider Pattern:** Utilize Astro's `