Created
July 28, 2025 12:47
-
-
Save N4N1T0/ee4118e1ff65e852efd4b207d5fe23c3 to your computer and use it in GitHub Desktop.
Best practices for building modern, performant websites with Astro.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| --- | |
| 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