Cursor rules for fast Astro 7 sites using HTML-first components, targeted client directives, server islands, prerendering and astro:assets images.
.cursor/rules/astro-performance-guidelines.mdc. Downloaded files need to be placed in that repository path.---
description: "Cursor rules for fast Astro 7 sites using HTML-first components, targeted client directives, server islands, prerendering and astro:assets images."
globs: "**/*"
alwaysApply: false
---
# Astro Performance Guidelines
Applies to: Astro pages, layouts and components (`**/*.astro`), framework islands used inside them, and `astro.config.*`.
Version baseline: checked against the Astro 7 documentation. Read `package.json` (`astro`, UI framework integrations) and `astro.config.*` (`output`, adapter, `image`) first, and follow the installed version.
## Ship HTML first, JavaScript only where needed
- `.astro` components render to HTML on the server or at build time. A UI framework component (React, Vue, Svelte, Solid, Preact) without a `client:*` directive is also rendered to HTML with no JavaScript.
- Add a `client:*` directive only to components that need interactivity, and put it on the smallest component that needs it rather than on a page-sized wrapper.
- Choose the latest hydration that still works for the user:
- `client:load` for controls needed immediately above the fold.
- `client:idle` for interactive UI that can wait until the browser is idle.
- `client:visible` for below-the-fold widgets such as carousels, comments and charts.
- `client:media="(max-width: 768px)"` for UI that only exists at some viewport sizes, such as a mobile menu.
- `client:only="react"` only for components that cannot render on the server (for example, ones that read `window` during render). Pass the framework name.
```astro
---
import Header from '../components/Header.astro';
import SearchBox from '../components/SearchBox.tsx';
import Comments from '../components/Comments.tsx';
---
<Header />
<SearchBox client:idle />
<article><slot /></article>
<Comments client:visible postId={Astro.props.postId} />
```
- Props passed to hydrated islands are serialized into the HTML; pass IDs and small values, not large objects.
- Prefer a `<script>` tag in an `.astro` component for small behaviors (toggle a class, copy to clipboard) instead of adding a framework island. Astro bundles and processes these scripts.
## Rendering mode
- Pages are prerendered at build time by default. Keep pages static unless they need per-request data.
- For per-request pages, add an adapter and set `export const prerender = false` on those pages only.
- For a mostly static page with one personalized or slow section, render that section as a server island with `server:defer` and give it fallback content, so the rest of the page stays static and cacheable.
```astro
---
import Avatar from '../components/Avatar.astro';
---
<Avatar server:defer>
<span slot="fallback">Loading profile</span>
</Avatar>
```
## Images
- Put local images in `src/` and render them with `<Image />` or `<Picture />` from `astro:assets`, so Astro can resize, convert and set `width`/`height`. Files in `public/` are copied as-is with no optimization.
- Every image needs `alt` (use `alt=""` for decorative images).
- Use `<Picture formats={['avif', 'webp']} />` when you need modern formats with a fallback.
- Allow remote image hosts in `image.domains` or `image.remotePatterns` in `astro.config.*` before optimizing remote images.
- `<Image />` renders `loading="lazy"` and `decoding="async"` by default. For the largest above-the-fold image, add the `priority` prop (Astro 5.10+), which sets eager loading and high fetch priority; on older versions set `loading="eager"` and `fetchpriority="high"` yourself.
```astro
---
import { Image } from 'astro:assets';
import hero from '../assets/hero.jpg';
---
<Image src={hero} alt="Team at the product launch" widths={[480, 960, 1440]} sizes="100vw" priority />
```
## Data and build work
- Fetch data in component frontmatter; on static pages this runs once per build, not per visitor.
- Use content collections for Markdown/MDX/JSON content so it is validated and loaded once.
- Do not import large client libraries into `.astro` frontmatter expecting tree-shaking on the client; frontmatter never ships to the browser, and island imports do.
## Caching and delivery
- Static assets in `_astro/` have hashed file names and can be cached long-term. Set cache headers through your host or adapter configuration.
- For on-demand pages, set `Cache-Control` explicitly with `Astro.response.headers.set(...)` when the response can be shared.
## Measure
- Check the JavaScript sent per page in the build output and the browser network panel after adding an island.
- Use Lighthouse or WebPageTest on the production build (`astro build && astro preview`), not on the dev server.
## Verify before finishing
Run these when they are available in the project:
- `npx astro check` (types and diagnostics in `.astro` files)
- `npx astro build`, then confirm that pages without islands load no framework JavaScript
- Lighthouse on `astro preview` for pages you changed
Cursor rules for Next.js 14+ App Router projects updated for Next.js 16 with async params, validated Route Handlers, generateMetadata and proxy.ts in place of middleware.
Cursor rules for strict TypeScript: compiler checks beyond strict, interfaces vs type aliases, real type guards, discriminated unions, utility types and branded IDs.
Cursor rules for Tailwind CSS v4: automatic class sorting, mobile-first breakpoints, CVA variants with merging, class-based dark mode via @custom-variant, and CSS-first @theme configuration.
Cursor
coding
Illustrative input and expected result. This example is a verification guide, not a recorded model run.
Content reviewed .
An Astro 7 blog with a few React components has this rule attached while a developer asks Cursor to add a search box, comments and a hero image to the post layout.
Add the React SearchBox and Comments components to src/layouts/Post.astro and a hero image from src/assets/hero.jpg. Comments are at the bottom of the page.
Illustrative; not a recorded model run. SearchBox hydrated with client:idle (or client:load if needed immediately), Comments with client:visible and only a postId prop; the hero rendered with <Image /> from astro:assets using an imported src/assets file, a descriptive alt, widths/sizes and priority; the page left prerendered; no getStaticProps, React.memo or other Next.js/React-only APIs.
Cursor applies project rules stored as .mdc files in .cursor/rules. Each rule's frontmatter (description, globs, alwaysApply) decides whether it is always included, attached to matching files, chosen by the agent from its description, or only used when you @-mention it. Cursor also reads AGENTS.md in the project root and subdirectories for plain-Markdown instructions.
.cursor/rules/astro-performance-guidelines.mdcStarting point, not a tested configuration for your repository. Attached when files matching the globs are in context (alwaysApply: false with globs). Review the instructions, adapt the file paths and run your project's existing checks.
---
description: "Astro Performance Guidelines: Islands, Rendering Modes and Images project conventions"
globs: "**/*"
alwaysApply: false
---
# Astro Performance Guidelines
Applies to: Astro pages, layouts and components (`**/*.astro`), framework islands used inside them, and `astro.config.*`.
Version baseline: checked against the Astro 7 documentation. Read `package.json` (`astro`, UI framework integrations) and `astro.config.*` (`output`, adapter, `image`) first, and follow the installed version.
## Ship HTML first, JavaScript only where needed
- `.astro` components render to HTML on the server or at build time. A UI framework component (React, Vue, Svelte, Solid, Preact) without a `client:*` directive is also rendered to HTML with no JavaScript.
- Add a `client:*` directive only to components that need interactivity, and put it on the smallest component that needs it rather than on a page-sized wrapper.
- Choose the latest hydration that still works for the user:
- `client:load` for controls needed immediately above the fold.
- `client:idle` for interactive UI that can wait until the browser is idle.
- `client:visible` for below-the-fold widgets such as carousels, comments and charts.
- `client:media="(max-width: 768px)"` for UI that only exists at some viewport sizes, such as a mobile menu.
- `client:only="react"` only for components that cannot render on the server (for example, ones that read `window` during render). Pass the framework name.
```astro
---
import Header from '../components/Header.astro';
import SearchBox from '../components/SearchBox.tsx';
import Comments from '../components/Comments.tsx';
---
<Header />
<SearchBox client:idle />
<article><slot /></article>
<Comments client:visible postId={Astro.props.postId} />
```
- Props passed to hydrated islands are serialized into the HTML; pass IDs and small values, not large objects.
- Prefer a `<script>` tag in an `.astro` component for small behaviors (toggle a class, copy to clipboard) instead of adding a framework island. Astro bundles and processes these scripts.
## Rendering mode
- Pages are prerendered at build time by default. Keep pages static unless they need per-request data.
- For per-request pages, add an adapter and set `export const prerender = false` on those pages only.
- For a mostly static page with one personalized or slow section, render that section as a server island with `server:defer` and give it fallback content, so the rest of the page stays static and cacheable.
```astro
---
import Avatar from '../components/Avatar.astro';
---
<Avatar server:defer>
<span slot="fallback">Loading profile</span>
</Avatar>
```
## Images
- Put local images in `src/` and render them with `<Image />` or `<Picture />` from `astro:assets`, so Astro can resize, convert and set `width`/`height`. Files in `public/` are copied as-is with no optimization.
- Every image needs `alt` (use `alt=""` for decorative images).
- Use `<Picture formats={['avif', 'webp']} />` when you need modern formats with a fallback.
- Allow remote image hosts in `image.domains` or `image.remotePatterns` in `astro.config.*` before optimizing remote images.
- `<Image />` renders `loading="lazy"` and `decoding="async"` by default. For the largest above-the-fold image, add the `priority` prop (Astro 5.10+), which sets eager loading and high fetch priority; on older versions set `loading="eager"` and `fetchpriority="high"` yourself.
```astro
---
import { Image } from 'astro:assets';
import hero from '../assets/hero.jpg';
---
<Image src={hero} alt="Team at the product launch" widths={[480, 960, 1440]} sizes="100vw" priority />
```
## Data and build work
- Fetch data in component frontmatter; on static pages this runs once per build, not per visitor.
- Use content collections for Markdown/MDX/JSON content so it is validated and loaded once.
- Do not import large client libraries into `.astro` frontmatter expecting tree-shaking on the client; frontmatter never ships to the browser, and island imports do.
## Caching and delivery
- Static assets in `_astro/` have hashed file names and can be cached long-term. Set cache headers through your host or adapter configuration.
- For on-demand pages, set `Cache-Control` explicitly with `Astro.response.headers.set(...)` when the response can be shared.
## Measure
- Check the JavaScript sent per page in the build output and the browser network panel after adding an island.
- Use Lighthouse or WebPageTest on the production build (`astro build && astro preview`), not on the dev server.
## Verify before finishing
Run these when they are available in the project:
- `npx astro check` (types and diagnostics in `.astro` files)
- `npx astro build`, then confirm that pages without islands load no framework JavaScript
- Lighthouse on `astro preview` for pages you changed
Ask for a small change to one matching file. Check the tool's loaded context or instruction view, inspect the diff for the requested convention, and run the relevant build or test. A well-formatted file does not guarantee the model follows every instruction.
Cursor's docs recommend keeping individual rules under 500 lines and splitting large rules into composable ones. Cursor's documentation states that it supports AGENTS.md in the project root and in subdirectories. Use AGENTS.md for plain-Markdown instructions you also want other tools to read, and .mdc rules when you need Cursor's scoping options (globs, description-based selection, always-apply).
Documentation checked 2026-11-05: Cursor docs: Rules
Cursor rules for Next.js 14+ App Router projects updated for Next.js 16 with async params, validated Route Handlers, generateMetadata and proxy.ts in place of middleware.
Cursor rules for strict TypeScript: compiler checks beyond strict, interfaces vs type aliases, real type guards, discriminated unions, utility types and branded IDs.
Cursor rules for Tailwind CSS v4: automatic class sorting, mobile-first breakpoints, CVA variants with merging, class-based dark mode via @custom-variant, and CSS-first @theme configuration.