agentleFS
dafthunk-comdafthunk-com/dafthunk/.cursor/rules/05-web-development.mdc

dafthunk / rules

Cursor rule133 starsChanged 9 months agoMITView on GitHub

Use the following format for code blocks: Good Example: React component using Tailwind and shadcn/ui Bad Example: Missing export, bad styling, incorrect file structure Good Example: Test file using Vitest Bad Example: Jest used instead of Vitest When fetching data in React components, use the library SWR (stale-while-revalidate) for its benefits in caching, revalidation, and focus tracking. The primary approach should be to consolidate related data fetching for a view into a single useSWR call. This approach is preferred when:…

The file

Show its rules and text

We split this 05-web-development.mdc into the rules it gives an agent

20 rules. About 0 are common, 16 are distinctive, and 4 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈16

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 4

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Always write complete, functional components using tsx with a named export function Component.

    line 10
  2. Distinctive:

    Use Tailwind CSS classes for styling and accessibility best practices.

    line 11
  3. Very specific:

    Prefer shadcn/ui and lucide-react if appropriate.

    line 12
  4. Distinctive:

    All code should work in a Vite + React + TypeScript setup.

    line 13
  5. Distinctive:

    Use Vitest for testing; include test blocks where relevant.

    line 15
  6. Very specific:

    Use import type when importing types from libraries.

    line 16
  7. Distinctive:

    Avoid dynamic imports, external APIs, or CDN usage unless requested.

    line 17
  8. Distinctive:

    Use the following format for code blocks:

    line 22
  9. Very specific:

    When fetching data in React components, use the library SWR (stale-while-revalidate) for its benefits in caching, revalidation, and focus …

    line 75
  10. Distinctive:

    All data is necessary for the component to render meaningfully.

    line 78
  11. Distinctive:

    Avoid by default. Ask: can this logic exist outside React?

    line 137
  12. Distinctive:

    Always include dependencies.

    line 138
  13. Distinctive:

    Use an inner async function if needed:

    line 139
  14. Distinctive:

    Avoid setting derived state inside useEffect.

    line 151
  15. Distinctive:

    Use only for expensive calculations to avoid unnecessary recalculations.

    line 154
  16. Distinctive:

    Not needed for simple or fast operations.

    line 155
  17. Distinctive:

    Use for storing mutable values that persist without triggering re-renders.

    line 168
  18. Distinctive:

    Don't overuse for general state—it can lead to bugs.

    line 170
  19. Distinctive:

    Never call hooks inside conditions, loops, or nested functions.

    line 173
  20. Distinctive:

    Always call hooks at the top level of your component.

    line 174

≈ “Common” is an estimate: at least 1 in 5 of the 3,030 Cursor rule files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
05-web-development.mdc
---
description: 
globs: apps/web/*
alwaysApply: false
---
# Web application

## Guidelines

1. Always write complete, functional components using `tsx` with a named `export function Component`.
2. Use Tailwind CSS classes for styling and accessibility best practices.
3. Prefer `shadcn/ui` and `lucide-react` if appropriate.
4. All code should work in a Vite + React + TypeScript setup.
5. Include only one file per code block. Avoid multi-file outputs.
6. Use Vitest for testing; include test blocks where relevant.
7. Use `import type` when importing types from libraries.
8. Avoid dynamic imports, external APIs, or CDN usage unless requested.
9. Escaped JSX characters like `<`, `>`, `{`, and `}` must be in string literals.

## Code block structure

Use the following format for code blocks:

```tsx project="ProjectName" file="path/to/file.tsx" type="react"
export function Component() {
  // your component here
}
```

## Examples

Good Example: React component using Tailwind and `shadcn/ui`
```tsx project="ButtonExample" file="Button.tsx" type="react"
import { Button } from "@/components/ui/button"

export function Component() {
  return <Button className="text-primary">Click me</Button>
}
```

Bad Example: Missing export, bad styling, incorrect file structure
```tsx
function Button() {
  return <button style={{ color: 'blue' }}>Click</button>
}
// Missing `export default`
// Uses inline styles instead of Tailwind
// No project/file/type metadata
```

Good Example: Test file using Vitest
```ts project="AddTest" file="add.test.ts" type="code"
import { describe, it, expect } from "vitest"
import { add } from "./add"

describe("add", () => {
  it("adds two numbers", () => {
    expect(add(2, 3)).toBe(5)
  })
})
```

Bad Example: Jest used instead of Vitest
```ts
import { test, expect } from "@jest/globals"

test("adds numbers", () => {
  expect(add(2, 3)).toBe(5)
})
// This is incorrect because the project uses Vitest, not Jest
```

## React Loading State Best Practices with SWR

When fetching data in React components, use the library SWR (`stale-while-revalidate`) for its benefits in caching, revalidation, and focus tracking. The primary approach should be to consolidate related data fetching for a view into a single `useSWR` call.

This approach is preferred when:
- All data is necessary for the component to render meaningfully.
- You want a unified loading state for a set of related data.
- Simplicity in state management for data fetching is desired.

Example: Using a single `useSWR` to fetch multiple related resources.
```tsx
import useSWR from 'swr';

export function MyDashboard() {
  // The key is an array of the individual resource keys/URLs.
  const { data, error, isLoading } = useSWR(
    ['/api/resourceA', '/api/resourceB'], // Unique keys for SWR
    async (keys: [string, string]) => {
        const [keyA, keyB] = keys;
        const [resourceA, resourceB] = await Promise.all([
            fetchResourceA(keyA),
            fetchResourceB(keyB),
        ]);
        return { resourceA, resourceB };
    };
  );

  if (isLoading) return <p>Loading dashboard data...</p>;
  if (error) return <p>Error loading data: {error.message}</p>;
  // data will be { resourceA: {...}, resourceB: {...} }

  return (
    <div>
      <h2>Dashboard</h2>
      {data && (
        <>
          <section>
            <h3>Resource A</h3>
            <p>{data.resourceA.content}</p>
          </section>
          <section>
            <h3>Resource B</h3>
            <p>{data.resourceB.data}</p>
          </section>
        </>
      )}
    </div>
  );
}
```

By using a single `useSWR` call with a fetcher that handles multiple requests (e.g., via `Promise.all`), you maintain a single `isLoading` and `error` state for the entire set of data required by the view. This simplifies the component logic and provides a more coherent loading experience.

Best Practice:
"Prefer showing meaningful parts of the UI as soon as possible by fetching all necessary data for a view with a single, consolidated `useSWR` call. This avoids janky partial loading and simplifies state management unless specific sections truly require independent data fetching lifecycles."

Avoid:
- Overcomplicating state logic with too many flags.
- Tightly coupling data fetching to UI components.
- Omitting proper error handling and fallback UIs.

## React Hook Usage Best Practices

useEffect:
- Avoid by default. Ask: can this logic exist outside React?
- Always include dependencies; don't silence lint rules.
- Use an inner async function if needed:

```tsx
useEffect(() => {
  const load = async () => {
    const res = await fetchData()
    setData(res)
  }
  load()
}, [])
```

- Avoid setting derived state inside useEffect. Prefer deriving it during render.

useMemo:
- Use only for expensive calculations to avoid unnecessary recalculations.
- Not needed for simple or fast operations.

```tsx
const filteredItems = useMemo(() => {
  return items.filter(i => i.active)
}, [items])
```

useCallback:
- Useful when passing callbacks to memoized child components.
- Prevents unnecessary re-renders due to function identity changes.

useRef:
- Use for storing mutable values that persist without triggering re-renders.
- Useful for DOM element references (e.g., input focus).
- Don't overuse for general state—it can lead to bugs.

General Rules:
- Never call hooks inside conditions, loops, or nested functions.
- Always call hooks at the top level of your component.
- Group and co-locate related hooks to make code easier to read and maintain.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.