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.

rulesmigrate-jsonhash-to-native.md

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

Stop Using jsonHash

Replace jsonHash from remix-utils with native Promise.all or data() patterns.

Why

  • jsonHash is a workaround for parallel async in loaders
  • Native Promise.all is clearer and doesn't need a dependency
  • data() with Single Fetch handles promises natively

Bad: jsonHash Pattern

import { jsonHash } from "remix-utils/json-hash";

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

  return jsonHash({
    items: client.items.list(),
    categories: client.categories.list(),
    user: client.users.current(),
  });
}

Good: Promise.all (Non-Streaming)

When all data is needed before render:

import { data } from "react-router";

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

  let [items, categories, user] = await Promise.all([
    client.items.list(),
    client.categories.list(),
    client.users.current(),
  ]);

  return data({ items, categories, user });
}

Good: data() with Promises (Streaming)

When some data can stream in later:

import { data } from "react-router";

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

  // Critical data - await immediately
  let items = await client.items.list();

  return data({
    items,
    // Non-critical data - streams automatically with Single Fetch
    categories: client.categories.list(),
    recommendations: client.recommendations.list(),
  });
}

Migration Examples

Simple Object Return

import { data } from "react-router";

// Before
return jsonHash({
  a: getA(),
  b: getB(),
});

// After
const [a, b] = await Promise.all([getA(), getB()]);
return data({ a, b });

Async Functions in jsonHash

import { data } from "react-router";

// Before
return jsonHash({
  async items() {
    let raw = await fetchItems();
    return transformItems(raw);
  },
  async metadata() {
    return fetchMetadata();
  },
});

// After
const [items, metadata] = await Promise.all([
  fetchItems().then(transformItems),
  fetchMetadata(),
]);
return data({ items, metadata });

Mixed Sync and Async

import { data } from "react-router";

// Before
return jsonHash({
  asyncData: fetchData(),
  syncData() {
    return computeSync();
  },
});

// After
const asyncData = await fetchData();
const syncData = computeSync();
return data({ asyncData, syncData });

When to Use Which Pattern

Scenario Pattern
All data critical, fast queries Promise.all + data()
Some data can load later data() with promises
Complex async transformations Promise.all + data()
Need to show partial UI fast data() with promises

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