All skills
sergiodxa avatar

/frontend-react-router-best-practices

@9da9532

React Router performance and architecture patterns. Use when writing loaders, actions, forms, routes, or working with React Router data fetching. Triggers on tasks involving React Router routes, data loading, form handling, or route organization.

Use this Skill: https://skilld.dev/gh/sergiodxa/agent-skills/frontend-react-router-best-practices

This session only. Nothing lands on disk.

rulesloader-colocate-queries.md

≈553 tokens on demand. Your agent reads this file only when SKILL.md points to it.

Colocate Data Queries

Keep data fetching functions in colocated queries.server.ts files next to route files.

Why

  • Keeps loaders clean and focused on orchestration
  • Makes queries reusable across loader and action
  • Server-only code stays separate from client code
  • Easier to test queries in isolation

File Structure

routes/
  my-feature/
    queries.server.ts    # Data fetching functions
    actions.server.ts    # Mutation functions (optional)
    route.tsx            # Loader, action, component
    components/          # Route-specific components
      header.tsx
      item-card.tsx

queries.server.ts

// queries.server.ts
import type { APIClient } from "~/lib/api.server";

export async function queryItems(client: APIClient) {
  let items = await client.items.list();
  return items.map((item) => ({
    id: item.id,
    title: item.title,
    slug: item.slug,
  }));
}

export async function queryFeaturedItems(client: APIClient) {
  return client.items.featured();
}

export async function querySuggestedItems(
  client: APIClient,
  existingItems: { id: string }[],
) {
  let suggestions = await client.items.suggested();
  let existingIds = new Set(existingItems.map((i) => i.id));
  return suggestions.filter((s) => !existingIds.has(s.id));
}

route.tsx

// route.tsx
import { data } from "react-router";
import {
  queryItems,
  queryFeaturedItems,
  querySuggestedItems,
} from "./queries.server";

export async function loader({ request, context }: Route.LoaderArgs) {
  let client = await authenticate(request);

  const [featuredItems, items] = await Promise.all([
    queryFeaturedItems(client),
    queryItems(client),
  ]);

  let suggestedItems = await querySuggestedItems(client, items);

  return data({ featuredItems, items, suggestedItems });
}

Benefits

  1. Loader stays focused: Orchestrates auth, parallel fetching, response
  2. Queries are testable: Can unit test query functions directly
  3. Reusable: Same query can be used in loader and action
  4. Clear separation: .server.ts suffix ensures server-only bundling

Source: SKILL.md on GitHub

2 warnings16d5 checks · Risk SAFE
  • Gen Agent Trust Hub16d

    The skill provides a comprehensive set of developer guidelines and best practices for building React Router applications. It emphasizes performance, type safety, and security measures like input validation (Zod), safe redirects, and CSRF protection (Sec-Fetch). No malicious code, obfuscation, or data exfiltration patterns were detected.

  • Socket16d

    1 alert: gptSecurity

  • Snyk16d

    Risk: LOW · No issues

  • Runlayer7mo

    56/56 files flagged

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at 9da9532. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 months ago.

Dormantupdated 8 months ago

README badge

README badge for sergiodxa/agent-skills/frontend-react-router-best-practices