mirror of
https://github.com/sanjeed5/awesome-cursor-rules-mdc.git
synced 2026-11-30 23:04:59 +08:00
Regenerate all MDC rules with fresh Exa data and December 2025 best practices
- Refreshed all 241 library rules with current best practices - Updated Exa search results for all libraries with 2025 content - Fixed --regenerate-all flag logic to properly process all libraries - Removed duplicate astro entry in rules.json (merged tags) - Added missing docker.mdc and vim.mdc files - All rules now include modern patterns and anti-patterns - Updated with framework-specific recommendations for 2025 Technical improvements: - Fixed retry_failed_only logic in generate_mdc_files.py - Improved progress tracking for regeneration scenarios - Ensured proper handling of --refresh-exa flag
This commit is contained in:
+335
-313
@@ -1,315 +1,337 @@
|
||||
---
|
||||
description: This rule file provides comprehensive best practices for Remix development, covering code organization, performance, security, testing, and more. It aims to guide developers in building maintainable, scalable, and secure Remix applications.
|
||||
globs: *.js,*.jsx,*.ts,*.tsx
|
||||
description: This guide outlines definitive best practices for building robust, performant, and maintainable applications with Remix, emphasizing co-location, type safety, and efficient data handling.
|
||||
globs: **/*.{js,jsx,ts,tsx}
|
||||
---
|
||||
## Remix Best Practices and Coding Standards
|
||||
|
||||
This document outlines the recommended best practices and coding standards for developing Remix applications. Following these guidelines will promote code consistency, maintainability, performance, and security.
|
||||
|
||||
### 1. Code Organization and Structure
|
||||
|
||||
#### 1.1. Directory Structure
|
||||
|
||||
A well-structured directory is crucial for maintainability and scalability. Here's a recommended structure:
|
||||
|
||||
|
||||
my-remix-app/
|
||||
├── app/
|
||||
│ ├── components/ # Reusable UI components
|
||||
│ │ ├── Button.tsx
|
||||
│ │ ├── Card.tsx
|
||||
│ │ └── ...
|
||||
│ ├── utils/ # Utility functions (e.g., date formatting, API helpers)
|
||||
│ │ ├── date-utils.ts
|
||||
│ │ ├── api.ts
|
||||
│ │ └── ...
|
||||
│ ├── services/ # Business logic and data access
|
||||
│ │ ├── auth.server.ts # Authentication logic (server-only)
|
||||
│ │ ├── user.server.ts # User data access (server-only)
|
||||
│ │ └── ...
|
||||
│ ├── routes/ # Remix route modules
|
||||
│ │ ├── _index.tsx # Index route
|
||||
│ │ ├── about.tsx # About page
|
||||
│ │ ├── blog/
|
||||
│ │ │ ├── $slug.tsx # Dynamic blog post route
|
||||
│ │ │ └── index.tsx # Blog index page
|
||||
│ │ ├── api/
|
||||
│ │ │ ├── auth.ts # API routes for authentication
|
||||
│ │ │ └── ...
|
||||
│ │ └── ...
|
||||
│ ├── styles/ # Global stylesheets
|
||||
│ │ ├── global.css
|
||||
│ │ └── ...
|
||||
│ ├── entry.client.tsx # Client-side entry point
|
||||
│ ├── entry.server.tsx # Server-side entry point
|
||||
│ ├── root.tsx # Root component (HTML structure)
|
||||
│ └── remix.env.d.ts
|
||||
├── public/ # Static assets (images, fonts, etc.)
|
||||
├── .gitignore
|
||||
├── jsconfig.json
|
||||
├── package-lock.json
|
||||
├── package.json
|
||||
├── remix.config.js
|
||||
└── tsconfig.json
|
||||
|
||||
|
||||
* `components`: Reusable UI elements. Separate presentational components from container components (smart vs. dumb components).
|
||||
* `utils`: Helper functions that are not specific to any React component. This promotes reusability and testability.
|
||||
* `services`: Business logic related files that handle server-side interactions.
|
||||
* `routes`: Defines the application's routes. Each file represents a route segment. Use nested routes for complex layouts and data dependencies.
|
||||
* `styles`: Global styles.
|
||||
* `public`: Static assets.
|
||||
|
||||
#### 1.2. File Naming Conventions
|
||||
|
||||
* **Components:** PascalCase (e.g., `Button.tsx`).
|
||||
* **Route Modules:** kebab-case (e.g., `about-us.tsx`). Use `$param` for dynamic route segments (e.g., `$postId.tsx`).
|
||||
* **Utility Functions:** camelCase (e.g., `formatDate.ts`).
|
||||
* **Stylesheets:** kebab-case (e.g., `global.css`).
|
||||
* Server only utilities that do not include UI (e.g `auth.server.ts`)
|
||||
|
||||
#### 1.3. Module Organization
|
||||
|
||||
* Group related components, utilities, and services into modules. A module is a directory containing files that work together to provide a specific functionality. This improves code discoverability and reduces naming conflicts.
|
||||
* Use index files (`index.ts` or `index.tsx`) to re-export members from a module, providing a single entry point.
|
||||
|
||||
|
||||
components/
|
||||
├── Button.tsx
|
||||
├── Input.tsx
|
||||
└── index.ts # export { Button } from './Button'; export { Input } from './Input';
|
||||
|
||||
|
||||
#### 1.4. Component Architecture
|
||||
|
||||
* **Presentational vs. Container Components:** Separate components that handle data fetching and state management (container components) from components that only render UI (presentational components). This promotes reusability and testability.
|
||||
* **Composition:** Favor composition over inheritance. Use React's `children` prop or render props to create flexible and reusable components.
|
||||
* **Controlled vs Uncontrolled Components:** Understand the difference between controlled and uncontrolled components. Controlled components manage their own state, while uncontrolled components rely on the DOM.
|
||||
|
||||
#### 1.5. Code Splitting
|
||||
|
||||
* Remix automatically handles route-based code splitting. Each route module is loaded independently, reducing the initial bundle size.
|
||||
* For larger components or modules, consider using dynamic imports (`React.lazy`) to further split your code. This is particularly helpful for features that are not immediately needed on page load.
|
||||
* Utilize Remix's built-in support for resource routes to handle data loading and background tasks separately, preventing them from blocking the main UI thread.
|
||||
|
||||
|
||||
### 2. Common Patterns and Anti-patterns
|
||||
|
||||
#### 2.1. Design Patterns
|
||||
|
||||
* **Compound Components:** Useful for components that need to share state or logic implicitly (e.g., Tabs, Accordions). Uses React Context to provide communication between parent and child components.
|
||||
* **Render Props/Function as Child:** Provides maximum flexibility by allowing the parent component to control the rendering of its children.
|
||||
* **Hooks:** Extract reusable stateful logic into custom hooks. This promotes code reuse and makes components more readable.
|
||||
* **Provider Pattern:** For managing global state or providing context to a subtree of components.
|
||||
|
||||
#### 2.2. Recommended Approaches
|
||||
|
||||
* **Data Loading:** Use Remix loaders for server-side data fetching. This ensures that data is available before the component renders, improving performance and SEO.
|
||||
* **Data Mutations:** Use Remix actions for handling form submissions and data updates. This centralizes data mutations and simplifies state management.
|
||||
* **Error Handling:** Implement error boundaries at the route level to catch errors and prevent the entire application from crashing.
|
||||
* **Authentication:** Implement authentication using server-side sessions or cookies. Avoid storing sensitive data in client-side storage.
|
||||
* **Authorization:** Implement authorization checks in loaders and actions to ensure that users only have access to authorized resources.
|
||||
|
||||
#### 2.3. Anti-patterns
|
||||
|
||||
* **Direct DOM Manipulation:** Avoid direct DOM manipulation using `document.querySelector` or `document.getElementById`. Use React's state management and rendering capabilities instead.
|
||||
* **Over-reliance on Client-Side State:** Utilize Remix's server-side capabilities to minimize client-side state management. This improves performance and reduces the risk of state inconsistencies.
|
||||
* **Ignoring Server-Side Rendering:** Take advantage of Remix's server-side rendering capabilities for improved performance and SEO. Don't perform all data fetching and rendering on the client-side.
|
||||
* **Complex Conditional Rendering in JSX:** Avoid deeply nested conditional rendering within JSX. Extract complex logic into separate functions or components.
|
||||
|
||||
#### 2.4. State Management
|
||||
|
||||
* Remix encourages server-side data fetching and mutations, reducing the need for complex client-side state management.
|
||||
* For simple component-level state, use React's `useState` hook.
|
||||
* For more complex application-level state, consider using Context API with `useReducer` or a state management library like Zustand or Jotai.
|
||||
* If needed, integrate third party state management libraries like Redux with caution, considering the benefits of Remix's built in data handling.
|
||||
|
||||
#### 2.5. Error Handling
|
||||
|
||||
* Utilize Remix's ErrorBoundary component to create dedicated error screens for routes. This provides a better user experience when errors occur.
|
||||
* Handle errors gracefully in loaders and actions. Return error responses or throw exceptions to trigger the error boundary.
|
||||
* Implement logging to track errors and diagnose issues.
|
||||
* Avoid try-catch blocks within components and rely on ErrorBoundaries for global exception handling.
|
||||
|
||||
|
||||
### 3. Performance Considerations
|
||||
|
||||
#### 3.1. Optimization Techniques
|
||||
|
||||
* **Minimize Bundle Size:** Remove unused code, optimize images, and use code splitting to reduce the initial bundle size.
|
||||
* **Optimize Data Fetching:** Fetch only the data that is needed for a specific route. Avoid over-fetching data.
|
||||
* **Cache Data:** Use HTTP caching or server-side caching to reduce the number of requests to the server.
|
||||
* **Memoization:** Use `React.memo` or `useMemo` to prevent unnecessary re-renders of components.
|
||||
* **Debouncing and Throttling:** Use debouncing and throttling to limit the frequency of event handlers, improving performance for user input and animations.
|
||||
|
||||
#### 3.2. Memory Management
|
||||
|
||||
* Avoid memory leaks by properly cleaning up event listeners and subscriptions.
|
||||
* Use the `useEffect` hook with a cleanup function to unsubscribe from subscriptions when a component unmounts.
|
||||
* Avoid storing large amounts of data in component state. Consider using a server-side data store or a more efficient data structure.
|
||||
|
||||
#### 3.3. Rendering Optimization
|
||||
|
||||
* Use the `shouldComponentUpdate` lifecycle method (or `React.memo`) to prevent unnecessary re-renders of components. Carefully analyze component re-renders with the React Profiler.
|
||||
* Virtualize long lists or tables to improve rendering performance.
|
||||
* Optimize CSS and avoid complex selectors that can slow down rendering.
|
||||
|
||||
#### 3.4. Bundle Size Optimization
|
||||
|
||||
* Use tools like `webpack-bundle-analyzer` or `rollup-plugin-visualizer` to analyze your bundle size and identify areas for optimization.
|
||||
* Remove unused dependencies and use tree shaking to eliminate dead code.
|
||||
* Use code splitting to load only the code that is needed for a specific route or component.
|
||||
|
||||
#### 3.5. Lazy Loading
|
||||
|
||||
* Use `React.lazy` to lazily load components that are not immediately needed on page load. This improves the initial load time.
|
||||
* Use Intersection Observer API to load images or other resources when they are visible in the viewport.
|
||||
|
||||
|
||||
### 4. Security Best Practices
|
||||
|
||||
#### 4.1. Common Vulnerabilities
|
||||
|
||||
* **Cross-Site Scripting (XSS):** Prevent XSS attacks by sanitizing user input and escaping HTML entities.
|
||||
* **Cross-Site Request Forgery (CSRF):** Protect against CSRF attacks by using anti-CSRF tokens in forms and API requests.
|
||||
* **SQL Injection:** Prevent SQL injection attacks by using parameterized queries or ORMs.
|
||||
* **Authentication and Authorization Issues:** Implement strong authentication and authorization mechanisms to protect sensitive data and resources.
|
||||
|
||||
#### 4.2. Input Validation
|
||||
|
||||
* Validate all user input on both the client-side and server-side.
|
||||
* Use a validation library like Zod or Yup to define schemas for your data.
|
||||
* Sanitize user input to remove potentially malicious characters or code.
|
||||
|
||||
#### 4.3. Authentication and Authorization
|
||||
|
||||
* Use a secure authentication protocol like OAuth 2.0 or OpenID Connect.
|
||||
* Store user credentials securely using hashing and salting.
|
||||
* Implement role-based access control (RBAC) to restrict access to sensitive resources.
|
||||
* Use server-side sessions or cookies for authentication. Avoid storing sensitive data in client-side storage.
|
||||
|
||||
#### 4.4. Data Protection
|
||||
|
||||
* Encrypt sensitive data at rest and in transit.
|
||||
* Use HTTPS to secure communication between the client and server.
|
||||
* Protect against data breaches by implementing strong access controls and monitoring for suspicious activity.
|
||||
* Implement data masking or anonymization to protect sensitive data in development and testing environments.
|
||||
|
||||
#### 4.5. Secure API Communication
|
||||
|
||||
* Use HTTPS to encrypt API communication.
|
||||
* Implement API rate limiting to prevent abuse.
|
||||
* Validate API requests and responses to prevent data injection attacks.
|
||||
* Use authentication tokens to authorize API requests.
|
||||
|
||||
|
||||
### 5. Testing Approaches
|
||||
|
||||
#### 5.1. Unit Testing
|
||||
|
||||
* Write unit tests for individual components, utilities, and services.
|
||||
* Use a testing framework like Jest or Mocha.
|
||||
* Mock dependencies to isolate the unit under test.
|
||||
* Test component rendering, state updates, and event handlers.
|
||||
|
||||
#### 5.2. Integration Testing
|
||||
|
||||
* Write integration tests to verify the interaction between different parts of the application.
|
||||
* Test data flow between components, loaders, and actions.
|
||||
* Use a testing library like React Testing Library to simulate user interactions.
|
||||
* Mock external APIs and services to ensure that integration tests are reliable.
|
||||
|
||||
#### 5.3. End-to-End Testing
|
||||
|
||||
* Write end-to-end tests to verify the entire application flow from the user's perspective.
|
||||
* Use a testing framework like Cypress or Playwright.
|
||||
* Test user authentication, data input, and navigation.
|
||||
* Run end-to-end tests in a continuous integration environment to ensure that the application is working as expected.
|
||||
|
||||
#### 5.4. Test Organization
|
||||
|
||||
* Organize tests in a directory structure that mirrors the application code.
|
||||
* Create separate test files for each component, utility, or service.
|
||||
* Use descriptive test names to clearly communicate the purpose of each test.
|
||||
* Keep tests small and focused to improve readability and maintainability.
|
||||
|
||||
#### 5.5. Mocking and Stubbing
|
||||
|
||||
* Use mocking and stubbing to isolate units under test and control their dependencies.
|
||||
* Use mocking libraries like Jest's `jest.fn()` or Mock Service Worker (MSW) to mock API responses and external services.
|
||||
* Avoid over-mocking, which can lead to tests that are not representative of the real application.
|
||||
|
||||
|
||||
### 6. Common Pitfalls and Gotchas
|
||||
|
||||
#### 6.1. Frequent Mistakes
|
||||
|
||||
* **Incorrectly Using Loaders and Actions:** Understanding the lifecycle and purpose of loaders and actions is crucial. Incorrect use can lead to performance issues and data inconsistencies.
|
||||
* **Ignoring Server-Side Rendering:** Failing to leverage Remix's server-side rendering capabilities can result in poor SEO and performance.
|
||||
* **Over-Complicating State Management:** Using complex state management libraries for simple applications can add unnecessary overhead.
|
||||
* **Not Validating User Input:** Failing to validate user input can lead to security vulnerabilities and data corruption.
|
||||
* **Using Browser Specific APIs in Server Code**: Only use web standard API that are available in Node.js in server code.
|
||||
|
||||
#### 6.2. Edge Cases
|
||||
|
||||
* **Handling Empty Data Sets:** Properly handle cases where loaders return empty data sets. Display appropriate messages to the user.
|
||||
* **Dealing with Network Errors:** Implement robust error handling to gracefully handle network errors and API failures.
|
||||
* **Managing User Sessions:** Implement secure session management to protect user data and prevent unauthorized access.
|
||||
* **Handling Concurrent Requests:** Be aware of potential race conditions when handling concurrent requests to the server.
|
||||
|
||||
#### 6.3. Version-Specific Issues
|
||||
|
||||
* Stay up-to-date with the latest Remix version and be aware of any breaking changes or bug fixes.
|
||||
* Consult the Remix documentation and release notes for information about version-specific issues.
|
||||
* Test your application thoroughly after upgrading to a new version of Remix.
|
||||
|
||||
#### 6.4. Compatibility Concerns
|
||||
|
||||
* Be aware of compatibility issues between Remix and other technologies, such as third-party libraries or server-side environments.
|
||||
* Test your application thoroughly in different environments to ensure that it is working as expected.
|
||||
* Use polyfills or shims to address compatibility issues when necessary.
|
||||
|
||||
#### 6.5. Debugging Strategies
|
||||
|
||||
* Use the browser's developer tools to debug client-side code.
|
||||
* Use server-side logging to track requests, responses, and errors.
|
||||
* Use a debugger to step through code and inspect variables.
|
||||
* Use profiling tools to identify performance bottlenecks.
|
||||
|
||||
|
||||
### 7. Tooling and Environment
|
||||
|
||||
#### 7.1. Recommended Tools
|
||||
|
||||
* **Code Editor:** VS Code, Sublime Text, or Atom with appropriate extensions for JavaScript/TypeScript, React, and Remix.
|
||||
* **Browser:** Chrome or Firefox with developer tools for debugging and performance analysis.
|
||||
* **Testing Framework:** Jest or Mocha.
|
||||
* **Testing Library:** React Testing Library or Enzyme.
|
||||
* **Linting:** ESLint with recommended Remix and React rules.
|
||||
* **Formatting:** Prettier.
|
||||
|
||||
#### 7.2. Build Configuration
|
||||
|
||||
* Use Remix's built-in build configuration for optimal performance.
|
||||
* Customize the build configuration as needed to optimize bundle size and performance.
|
||||
* Use environment variables to configure the application for different environments.
|
||||
|
||||
#### 7.3. Linting and Formatting
|
||||
|
||||
* Use ESLint to enforce code style and prevent errors.
|
||||
* Use Prettier to automatically format code for consistency.
|
||||
* Configure ESLint and Prettier to work together seamlessly.
|
||||
* Use a pre-commit hook to run ESLint and Prettier before committing code.
|
||||
|
||||
#### 7.4. Deployment
|
||||
|
||||
* Deploy Remix applications to a serverless environment like Vercel or Netlify for optimal performance and scalability.
|
||||
* Use a containerization platform like Docker to package and deploy the application.
|
||||
* Use a CDN to cache static assets and improve delivery speed.
|
||||
|
||||
#### 7.5. CI/CD Integration
|
||||
|
||||
* Use a continuous integration/continuous deployment (CI/CD) pipeline to automate the build, test, and deployment process.
|
||||
* Use a CI/CD platform like GitHub Actions or GitLab CI.
|
||||
* Automate code linting, formatting, and testing in the CI/CD pipeline.
|
||||
* Automate deployment to different environments in the CI/CD pipeline.
|
||||
# Remix Best Practices
|
||||
|
||||
Remix provides a powerful full-stack framework built on React Router v7. Adhering to these guidelines ensures a consistent, high-quality codebase that leverages Remix's strengths for server-rendered applications.
|
||||
|
||||
## 1. Code Organization and Structure
|
||||
|
||||
**Always co-locate route logic and UI.** Each route must live in its own folder, containing its `route.tsx` component, `loader.ts`, `action.ts`, and optional `meta.ts` or `error-boundary.tsx`. This keeps related concerns together and simplifies navigation.
|
||||
|
||||
❌ BAD:
|
||||
```tsx
|
||||
// app/data/posts.ts
|
||||
export async function getPost(id: string) { /* ... */ }
|
||||
|
||||
// app/components/PostDetail.tsx
|
||||
import { getPost } from '~/data/posts';
|
||||
function PostDetail() {
|
||||
// Client-side fetch or prop drilling
|
||||
}
|
||||
|
||||
// app/routes/posts.$postId.tsx
|
||||
import PostDetail from '~/components/PostDetail';
|
||||
export default function PostRoute() {
|
||||
return <PostDetail />;
|
||||
}
|
||||
```
|
||||
|
||||
✅ GOOD:
|
||||
```tsx
|
||||
// app/routes/posts.$postId/route.tsx
|
||||
import { json, useLoaderData } from "@remix-run/node";
|
||||
import type { LoaderFunctionArgs } from "@remix-run/node";
|
||||
|
||||
export async function loader({ params }: LoaderFunctionArgs) {
|
||||
const post = await db.getPost(params.postId); // Direct server call
|
||||
if (!post) throw new Response("Not Found", { status: 404 });
|
||||
return json({ post });
|
||||
}
|
||||
|
||||
export default function PostDetailRoute() {
|
||||
const { post } = useLoaderData<typeof loader>();
|
||||
return (
|
||||
<div>
|
||||
<h1>{post.title}</h1>
|
||||
<p>{post.content}</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// app/routes/posts.$postId/error-boundary.tsx
|
||||
import { isRouteErrorResponse, useRouteError } from "@remix-run/react";
|
||||
export function ErrorBoundary() {
|
||||
const error = useRouteError();
|
||||
if (isRouteErrorResponse(error)) {
|
||||
return <div>Error: {error.status} {error.statusText}</div>;
|
||||
}
|
||||
return <div>Unknown Error!</div>;
|
||||
}
|
||||
```
|
||||
|
||||
**Maintain a clear `src` directory hierarchy.** Separate concerns into logical folders like `app/components`, `app/features`, `app/hooks`, `app/utils`, `app/services`, etc.
|
||||
|
||||
❌ BAD:
|
||||
```
|
||||
app/
|
||||
├── index.tsx
|
||||
├── post-list.tsx
|
||||
├── user-profile.tsx
|
||||
├── api.ts
|
||||
└── helpers.ts
|
||||
```
|
||||
|
||||
✅ GOOD:
|
||||
```
|
||||
app/
|
||||
├── components/ // Reusable UI components
|
||||
├── features/ // Domain-specific components/logic
|
||||
├── hooks/ // Custom React hooks
|
||||
├── routes/ // File-based routes (as above)
|
||||
├── services/ // Database/API interaction logic
|
||||
├── utils/ // Generic utility functions
|
||||
└── entry.client.tsx
|
||||
└── entry.server.tsx
|
||||
```
|
||||
|
||||
**Enforce consistent naming conventions.** Use PascalCase for React components and types, and kebab-case for file names.
|
||||
|
||||
❌ BAD: `mycomponent.jsx`, `MyComponent.ts`, `userProfile.tsx`
|
||||
✅ GOOD: `MyComponent.tsx`, `user-profile.ts`, `UserProfile.tsx`
|
||||
|
||||
## 2. Common Patterns and Anti-patterns
|
||||
|
||||
**Use Remix `Form` for all data mutations.** This leverages Remix's built-in revalidation and pending UI states for a robust user experience.
|
||||
|
||||
❌ BAD:
|
||||
```tsx
|
||||
import { useState } from 'react';
|
||||
function NewPostForm() {
|
||||
const [title, setTitle] = useState('');
|
||||
const handleSubmit = async (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
await fetch('/posts', { method: 'POST', body: JSON.stringify({ title }) });
|
||||
};
|
||||
return <form onSubmit={handleSubmit}>...</form>;
|
||||
}
|
||||
```
|
||||
|
||||
✅ GOOD:
|
||||
```tsx
|
||||
// app/routes/posts.new/route.tsx
|
||||
import { Form, redirect } from "@remix-run/react";
|
||||
import type { ActionFunctionArgs } from "@remix-run/node";
|
||||
|
||||
export async function action({ request }: ActionFunctionArgs) {
|
||||
const formData = await request.formData();
|
||||
const title = formData.get("title");
|
||||
await db.createPost({ title });
|
||||
return redirect("/posts");
|
||||
}
|
||||
|
||||
export default function NewPostForm() {
|
||||
return (
|
||||
<Form method="post">
|
||||
<input type="text" name="title" />
|
||||
<button type="submit">Create Post</button>
|
||||
</Form>
|
||||
);
|
||||
}
|
||||
```
|
||||
|
||||
**Utilize Resource Routes for API-only endpoints.** For data fetching or mutations not tied to a specific UI page, create a resource route.
|
||||
|
||||
❌ BAD: Creating a separate API server or `app/api/posts.ts` that isn't a route.
|
||||
✅ GOOD:
|
||||
```tsx
|
||||
// app/routes/api.posts.$postId.ts
|
||||
import { json } from "@remix-run/node";
|
||||
import type { LoaderFunctionArgs } from "@remix-run/node";
|
||||
|
||||
export async function loader({ params }: LoaderFunctionArgs) {
|
||||
const post = await db.getPost(params.postId);
|
||||
return json(post);
|
||||
}
|
||||
```
|
||||
|
||||
## 3. Performance Considerations
|
||||
|
||||
**Perform all heavy data fetching in `loader` and `action` functions.** These run exclusively on the server, keeping your client bundle small and fast.
|
||||
|
||||
❌ BAD:
|
||||
```tsx
|
||||
// app/routes/dashboard.tsx
|
||||
import { useEffect, useState } from 'react';
|
||||
function Dashboard() {
|
||||
const [data, setData] = useState(null);
|
||||
useEffect(() => {
|
||||
fetch('/api/dashboard-data').then(res => res.json()).then(setData);
|
||||
}, []);
|
||||
return <div>{data ? 'Loaded' : 'Loading...'}</div>;
|
||||
}
|
||||
```
|
||||
|
||||
✅ GOOD:
|
||||
```tsx
|
||||
// app/routes/dashboard.tsx
|
||||
import { json, useLoaderData } from "@remix-run/node";
|
||||
export async function loader() {
|
||||
const data = await db.getDashboardData(); // Server-side data fetch
|
||||
return json(data);
|
||||
}
|
||||
export default function Dashboard() {
|
||||
const data = useLoaderData<typeof loader>();
|
||||
return <div>{data ? 'Loaded' : 'Loading...'}</div>;
|
||||
}
|
||||
```
|
||||
|
||||
**Defer non-critical data with `defer` and `Await` for faster initial page loads.** This allows critical data to render immediately while slower data streams in.
|
||||
|
||||
❌ BAD: Waiting for all data to resolve before rendering.
|
||||
```tsx
|
||||
// loader that waits for everything
|
||||
export async function loader() {
|
||||
const critical = await getCriticalData();
|
||||
const slow = await getSlowData(); // Blocks initial render
|
||||
return json({ critical, slow });
|
||||
}
|
||||
```
|
||||
|
||||
✅ GOOD:
|
||||
```tsx
|
||||
// loader that streams slow data
|
||||
import { defer, Await, json } from "@remix-run/node";
|
||||
import { Suspense } from "react";
|
||||
|
||||
export async function loader() {
|
||||
const critical = await getCriticalData();
|
||||
const slowPromise = getSlowData(); // Returns a promise
|
||||
return defer({ critical, slow: slowPromise });
|
||||
}
|
||||
|
||||
export default function MyRoute() {
|
||||
const { critical, slow } = useLoaderData<typeof loader>();
|
||||
return (
|
||||
<div>
|
||||
<h1>Critical: {critical.message}</h1>
|
||||
<Suspense fallback={<p>Loading slow data...</p>}>
|
||||
<Await resolve={slow}>
|
||||
{(resolvedSlow) => <p>Slow: {resolvedSlow.message}</p>}
|
||||
</Await>
|
||||
</Suspense>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
```
|
||||
|
||||
## 4. Common Pitfalls and Gotchas
|
||||
|
||||
**Avoid client-side specific APIs in `loader` or `action` functions.** These functions run on the server, so `window`, `localStorage`, `document`, etc., are unavailable.
|
||||
|
||||
❌ BAD:
|
||||
```tsx
|
||||
export async function loader() {
|
||||
const token = window.localStorage.getItem('token'); // Will fail on server
|
||||
// ...
|
||||
}
|
||||
```
|
||||
|
||||
✅ GOOD: Pass necessary data via cookies, headers, or form data.
|
||||
```tsx
|
||||
export async function loader({ request }: LoaderFunctionArgs) {
|
||||
const cookieHeader = request.headers.get("Cookie");
|
||||
const token = getAuthTokenFromCookie(cookieHeader); // Server-safe
|
||||
// ...
|
||||
}
|
||||
```
|
||||
|
||||
**Treat `useLoaderData` and `useActionData` as immutable.** Remix revalidates data, so direct mutations won't persist and can lead to unexpected behavior.
|
||||
|
||||
❌ BAD:
|
||||
```tsx
|
||||
const { items } = useLoaderData<typeof loader>();
|
||||
items.push("new item"); // Don't mutate loader data directly
|
||||
```
|
||||
|
||||
✅ GOOD: Create a new array or object if you need to modify data client-side.
|
||||
```tsx
|
||||
const { items } = useLoaderData<typeof loader>();
|
||||
const [clientItems, setClientItems] = useState(items);
|
||||
// ... setClientItems([...clientItems, "new item"])
|
||||
```
|
||||
|
||||
## 5. Hooks Best Practices
|
||||
|
||||
**Use `useNavigation` for pending UI states.** This provides a seamless user experience during transitions.
|
||||
|
||||
❌ BAD: Manually managing `isLoading` state with `useState` for form submissions.
|
||||
✅ GOOD:
|
||||
```tsx
|
||||
import { Form, useNavigation } from "@remix-run/react";
|
||||
export default function MyForm() {
|
||||
const navigation = useNavigation();
|
||||
const isSubmitting = navigation.state === "submitting";
|
||||
return (
|
||||
<Form method="post">
|
||||
<button type="submit" disabled={isSubmitting}>
|
||||
{isSubmitting ? "Saving..." : "Save"}
|
||||
</button>
|
||||
</Form>
|
||||
);
|
||||
}
|
||||
```
|
||||
|
||||
**Use `useFetcher` for non-navigation data mutations or fetches.** This is ideal for background updates, like liking a post, without triggering a full page navigation.
|
||||
|
||||
❌ BAD: Using `<Form>` for every small interaction that doesn't need a redirect.
|
||||
✅ GOOD:
|
||||
```tsx
|
||||
import { useFetcher } from "@remix-run/react";
|
||||
export default function LikeButton({ postId }: { postId: string }) {
|
||||
const fetcher = useFetcher();
|
||||
const isLiking = fetcher.state === "submitting";
|
||||
return (
|
||||
<fetcher.Form method="post" action="/api/like">
|
||||
<input type="hidden" name="postId" value={postId} />
|
||||
<button type="submit" disabled={isLiking}>
|
||||
{isLiking ? "Liking..." : "Like"}
|
||||
</button>
|
||||
</fetcher.Form>
|
||||
);
|
||||
}
|
||||
```
|
||||
|
||||
## 6. Type Safety with TypeScript
|
||||
|
||||
**Always use TypeScript with Remix.** Leverage `typeof loader` and `typeof action` for type-safe data access, ensuring robust code and improved developer experience.
|
||||
|
||||
❌ BAD:
|
||||
```tsx
|
||||
// No types, relies on runtime checks
|
||||
function MyComponent() {
|
||||
const data = useLoaderData(); // data is 'any'
|
||||
// ...
|
||||
}
|
||||
```
|
||||
|
||||
✅ GOOD:
|
||||
```tsx
|
||||
import { json, useLoaderData } from "@remix-run/node";
|
||||
export async function loader() {
|
||||
return json({ message: "Hello", count: 42 });
|
||||
}
|
||||
export default function MyComponent() {
|
||||
const data = useLoaderData<typeof loader>(); // data is { message: string; count: number; }
|
||||
return <p>{data.message} {data.count}</p>;
|
||||
}
|
||||
```
|
||||
|
||||
## 7. Security
|
||||
|
||||
**Implement rate-limiting for critical endpoints.** Protect your `action` and `resource` routes from abuse. This is typically done at the edge (Cloudflare, Vercel) or with server-side middleware.
|
||||
|
||||
❌ BAD: No protection for public-facing mutation endpoints.
|
||||
✅ GOOD:
|
||||
```ts
|
||||
// app/routes/signup.ts
|
||||
import { json, ActionFunctionArgs } from "@remix-run/node";
|
||||
import { rateLimit } from "~/utils/rate-limiter.server"; // Custom server utility
|
||||
|
||||
export async function action({ request }: ActionFunctionArgs) {
|
||||
await rateLimit(request, "signup", { limit: 5, window: 60 }); // 5 requests per minute
|
||||
// ... signup logic
|
||||
return json({ success: true });
|
||||
}
|
||||
```
|
||||
Reference in New Issue
Block a user