Skip to content

Instantly share code, notes, and snippets.

@N4N1T0
Created July 28, 2025 12:47
Show Gist options
  • Save N4N1T0/ee4118e1ff65e852efd4b207d5fe23c3 to your computer and use it in GitHub Desktop.
Save N4N1T0/ee4118e1ff65e852efd4b207d5fe23c3 to your computer and use it in GitHub Desktop.
Best practices for building modern, performant websites with Astro.
---
description: Best practices for building modern, performant websites with Astro.
globs: ["src/**/*.astro", "src/**/*.ts", "src/**/*.tsx"]
alwaysApply: false
---
# πŸš€ Astro Modern Practices
## 🧠 Core Philosophy
- Island architecture: ship less JS by default.
- Prefer `.astro` files for content-driven UI.
- Use client components (`<Component client:load />`) only when needed.
## 🧱 Components
- Use `.astro` for static/UI-first components.
- Use React/Svelte/Vue only when interactivity is required.
- Pass props explicitly; no global state by default.
## 🌍 Routing
- File-based routing: `src/pages/about.astro` β†’ `/about`.
- Use dynamic routes: `src/pages/blog/[slug].astro`.
- Add `404.astro` for custom not found pages.
## πŸ“¦ Content Collections
- Use `src/content/` + `defineCollection()` for blogs, docs, etc.
- Type-safe frontmatter with schemas.
- Prefer Markdown/MDX over pure `.astro` for content-heavy pages.
## πŸ“„ SEO & Metadata
- Use `<title>`, `<meta>`, and Open Graph tags in layouts or pages.
- Generate sitemaps with `astro-sitemap`.
- Use canonical URLs, alt text, and semantic HTML.
## 🧩 Layouts
- Create base layouts in `src/layouts/`.
- Use `<slot />` and named slots for flexibility.
- Co-locate page layout with its route structure.
## πŸ’¨ Styling
- Use Tailwind CSS or scoped styles in `.astro` files.
- Use `:global()` only when absolutely needed.
- Prefer utility-first styling over long class chains.
## πŸ”Œ Integrations
- Enable only what's needed: `@astrojs/image`, `@astrojs/mdx`, `@astrojs/sitemap`, etc.
- Configure integrations in `astro.config.mjs`.
## βš™οΈ Scripts & Interactivity
- Prefer `client:load` or `client:visible` for progressive enhancement.
- Avoid unnecessary hydration (`client:only` for full islands).
- No hydration = no bundle β€” default behavior is optimal.
## 🌍 Images & Assets
- Use `astro/assets` pipeline (`<Image src={...} />`) for optimization.
- Set responsive sizes and `alt` attributes.
- Use SVGs inline for icons when possible.
## πŸ— File Structure
- src/
- pages/ # routes
- components/ # UI units
- layouts/ # page scaffolds
- content/ # Markdown/MDX + schemas
- styles/ # Tailwind or global CSS
- lib/ # utils, fetchers
## πŸ§ͺ Testing
- Use Playwright for integration/E2E.
- Use Vitest for logic/components.
- Prefer testing rendered HTML/output over logic.
## πŸ“ˆ Performance
- Use Lighthouse or Web Vitals tools.
- Keep JS usage minimal (Astro does it by default).
- Lazy-load images and hydrate only interactive elements.
## πŸ›‘ Security
- Escape user-generated content (Markdown, forms).
- Use `Content-Security-Policy` headers.
- Avoid inline JS unless trusted and controlled.
## β™Ώ Accessibility
- Use semantic HTML.
- Test keyboard navigation and screen reader flow.
- Lint with `eslint-plugin-jsx-a11y` if using JSX components.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment