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.

rulesroute-handle-metadata.md

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

Route Handle Metadata

Use handle export with app-defined handle types for route metadata.

Why

  • Control client-side hydration behavior
  • Add external scripts per route
  • Configure layout features (banners, CTAs, aside panels)
  • Type-safe handle configuration

Handle Types

Define typed handle interfaces in your app (for example, Handle, LayoutHandle, AppHandle) and use them consistently across routes.

Handle (Base)

The basic handle for all routes:

export const handle: Handle = {
  // Enable client-side JavaScript for this route
  hydrate: true,
};

// Or conditionally based on loader data
export const handle: Handle<typeof loader> = {
  hydrate: (data) => data.needsInteractivity,
};

LayoutHandle

For routes within the main layout:

export const handle: LayoutHandle<typeof loader> = {
  hydrate: true,
  stickyHeader: true,
  footerType: "app", // "app" | "public"
  canvasColor: "white", // "white" | "neutral"

  // Dynamic banner above header
  banner: (data) =>
    data.showBanner
      ? {
          title: "Special offer",
          link: "/offer",
        }
      : null,

  // CTA buttons in header
  mainCTA: (data) => ({
    label: "Upgrade",
    to: "/upgrade",
  }),
  secondaryCTA: (data) => ({
    label: "Contact Sales",
    to: "/contact",
  }),
};

AppHandle

For authenticated app routes (extends LayoutHandle):

export const handle: AppHandle<typeof loader> = {
  hydrate: true,
  footerType: "app",

  // Render a component in the right aside column
  Aside: (data) => <SidebarWidget data={data} />,
};

Common Patterns

Hydration Control

// Always hydrate (needs JS)
export const handle: Handle = {
  hydrate: true,
};

// Never hydrate (static page)
export const handle: Handle = {
  hydrate: false,
};

// Conditional hydration
export const handle: Handle<typeof loader> = {
  hydrate: (data) => data.hasInteractiveFeatures,
};

External Scripts

export const handle: Handle<typeof loader> = {
  hydrate: true,
  scripts: (data) => [
    {
      src: "https://example.com/widget.js",
      async: true,
    },
  ],
};

Layout Configuration

export const handle: LayoutHandle = {
  hydrate: true,
  stickyHeader: true,
  canvasColor: "neutral",
  footerType: "public",
};

App Route with Aside

export const handle: AppHandle<typeof loader> = {
  hydrate: true,
  Aside: (data) => (
    <aside className="p-4">
      <h2>Related Items</h2>
      {data.relatedItems.map((item) => (
        <ItemCard key={item.id} item={item} />
      ))}
    </aside>
  ),
};

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