nextjs
# Next.js Template Skill
What this skill does
# Next.js Template Skill
Generate production-ready Next.js pages with SSR initial load, flexible client-side data management, and server-response-based updates.
## When to Use This Skill
Use this skill when asked to:
- Create a new Next.js page with data table
- Add CRUD functionality to a Next.js application
- Generate pages with server-side rendering and client-side updates
- Build admin/settings pages with filtering, pagination, and bulk actions
## Architecture Overview
```
┌─────────────────────────────────────────────────────────────┐
│ Browser Request │
└──────────────────────────┬──────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ page.tsx (Server Component) │
│ • auth() check │
│ • await searchParams (Next.js 15+) │
│ • Fetch initial data via server actions │
│ • Pass initialData to client component │
└──────────────────────────┬──────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ *-table.tsx (Client Component) │
│ • useSWR with fallbackData: initialData │
│ • Context Provider for actions │
│ • updateItems() uses server response (NOT optimistic) │
└──────────────────────────┬──────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ API Routes (app/api/...) │
│ • withAuth() wrapper │
│ • Proxy to FastAPI backend │
│ • backendFetch() direct calls to FastAPI backend │
└─────────────────────────────────────────────────────────────┘
```
## File Structure
```
app/(pages)/setting/{entity}/
├── page.tsx # SSR page (server component)
├── context/
│ └── {entity}-actions-context.tsx # Actions provider
└── _components/
├── actions/
│ ├── add-{entity}-button.tsx
│ └── {entity}-actions-menu.tsx
├── modal/
│ ├── add-{entity}-sheet.tsx
│ └── edit-{entity}-sheet.tsx
├── sidebar/
│ └── status-panel.tsx
└── table/
├── {entity}-table.tsx # Main client component
├── {entity}-table-body.tsx
└── {entity}-table-columns.tsx
app/api/setting/{entity}/
├── route.ts # GET (list), POST (create)
└── [{entityId}]/
├── route.ts # GET, PUT, DELETE
└── status/
└── route.ts # PUT (toggle status)
lib/
├── actions/
│ └── {entity}.actions.ts # Server actions
└── fetch/
├── client.ts # Client-side fetch (api.get/post/put/delete)
├── server.ts # Server action fetch (serverGet/Post/Put/Delete)
├── backend.ts # backendFetch() for API routes
└── api-route-helper.ts # withAuth() wrapper for API routes
lib/
├── types/
│ └── api/
│ └── {entity}.ts # TypeScript types
```
## Core Principles
### 1. Data Fetching Strategy
Choose the appropriate pattern based on requirements. See [references/data-fetching-strategy.md](references/data-fetching-strategy.md) for the decision framework.
**Server Component (page.tsx) - Same for both strategies:**
```tsx
// NO "use client" - this is a server component
import { auth } from "@/lib/auth/server-auth";
import { getItems } from "@/lib/actions/items.actions";
import { redirect } from "next/navigation";
import ItemsTable from "./_components/table/items-table";
export default async function ItemsPage({
searchParams,
}: {
searchParams: Promise<{ page?: string; limit?: string; search?: string }>;
}) {
const session = await auth();
if (!session?.accessToken) redirect("/login");
const params = await searchParams; // Next.js 15+ requires await
const page = Number(params.page) || 1;
const limit = Number(params.limit) || 10;
const items = await getItems(limit, (page - 1) * limit, params);
return <ItemsTable initialData={items} />;
}
```
**Strategy A: Simple Fetching (Default)**
```tsx
"use client";
import { useState } from "react";
import { api } from "@/lib/fetch/client";
function ItemsTable({ initialData }) {
const [data, setData] = useState(initialData);
// Update from server response
const updateItems = (serverResponse: Item[]) => {
setData(current => ({
...current,
items: current.items.map(item => {
const updated = serverResponse.find(i => i.id === item.id);
return updated ?? item;
}),
}));
};
// ...
}
```
See [references/simple-fetching-pattern.md](references/simple-fetching-pattern.md).
**Strategy B: SWR Fetching (When Justified)**
```tsx
"use client";
import useSWR from "swr";
import { api } from "@/lib/fetch/client";
const fetcher = (url: string) => api.get(url);
function ItemsTable({ initialData }) {
const searchParams = useSearchParams();
const page = Number(searchParams?.get("page") || "1");
/**
* SWR JUSTIFICATION:
* - Reason: [Document why revalidation needed]
* - Trigger: [Interval / Focus / Manual]
*/
const { data, mutate, isLoading } = useSWR(
`/api/setting/items?page=${page}`,
fetcher,
{
fallbackData: initialData,
keepPreviousData: true,
revalidateOnMount: false,
revalidateOnFocus: false, // Set true only if justified
}
);
// ...
}
```
See [references/swr-fetching-pattern.md](references/swr-fetching-pattern.md).
### 2. Server Response Updates (NOT Optimistic)
```tsx
// ✅ CORRECT: Use server response to update cache
const updateItems = async (serverResponse: Item[]) => {
const currentData = data;
if (!currentData) return;
const responseMap = new Map(serverResponse.map(i => [i.id, i]));
const updatedList = currentData.items.map(item =>
responseMap.has(item.id) ? responseMap.get(item.id)! : item
);
await mutate(
{ ...currentData, items: updatedList },
{ revalidate: false }
);
};
// In action handler:
const updatedItem = await api.put<Item>(`/setting/items/${id}`, payload);
await updateItems([updatedItem]); // Use server response!
```
```tsx
// ❌ WRONG: Optimistic update
const updatedList = currentData.items.map(item =>
item.id === id ? { ...item, ...localChanges } : item // Don't do this!
);
```
### 3. Context Pattern for Actions
```tsx
// context/{entity}-actions-context.tsx
"use client";
import { createContext, useContext, ReactNode } from "react";
interface ActionsContextType {
onToggleStatus: (id: string, isActive: boolean) => Promise<ActionResult>;
onUpdate: (id: string, data: UpdateData) => Promise<ActionResult>;
updateItems: (items: Item[]) => Promise<void>;
onRefresh: () => Promise<void>;
}
const ActionsContext = createContext<ActionsContextType | null>(null);
export function ActionsProvider({ children, actions }: Props) {
return (
<ActionsContext.Provider value={actions}>
{children}
</ActionsContext.Provider>
);
}
export function useTableActions() {
const context = useContext(ActionsContext);
if (!context) {
throw new Error("useTableActions must be used within ActionsProvider");
}
return context;
}
```
### 4. API Route Pattern
```tsx
// app/api/setting/items/route.ts
import { NextRequest } from "next/server";
import { withAuth } from "@/lib/fetch/api-route-helper";
import { backendFetch } from "@/lib/fetch/backend";
export async function GET(request: NextRequest) {
const params = request.nextUrl.searchParams.toString();
return withAuth((token) => backendFetch(`/setting/items/?${params}`, token));
}
export async function POST(request: NextRequestRelated in Web Dev
generating-lwc-components
IncludedLightning Web Components with PICKLES methodology and 165-point scoring. Use this skill when the user creates or edits LWC components, builds wire service patterns, or writes Jest tests for LWC. TRIGGER when: user creates/edits LWC components, touches lwc/**/*.js, .html, .css, .js-meta.xml files, or asks about wire service, SLDS, or Jest LWC tests. DO NOT TRIGGER when: Apex classes (use generating-apex), Aura components, or Visualforce.
tanstack-query
IncludedManage server state in React with TanStack Query v5. Set up queries with useQuery, mutations with useMutation, configure QueryClient caching strategies, implement optimistic updates, and handle infinite scroll with useInfiniteQuery. Use when: setting up data fetching in React projects, migrating from v4 to v5, or fixing object syntax required errors, query callbacks removed issues, cacheTime renamed to gcTime, isPending vs isLoading confusion, keepPreviousData removed problems.
document-processor-api
IncludedProcess documents with Nutrient DWS. Use when the user wants to generate PDFs from HTML or URLs, convert Office/images/PDFs, assemble or split packets, OCR scans, extract text/tables/key-value pairs, redact PII, watermark, sign, fill forms, optimize PDFs, or produce compliance outputs like PDF/A or PDF/UA. Triggers include convert to PDF, merge these PDFs, OCR this scan, extract tables, redact PII, sign this PDF, make this PDF/A, or linearize for web delivery.
nutrient-document-processing
IncludedProcess documents with Nutrient DWS. Use when the user wants to generate PDFs from HTML or URLs, convert Office/images/PDFs, assemble or split packets, OCR scans, extract text/tables/key-value pairs, redact PII, watermark, sign, fill forms, optimize PDFs, or produce compliance outputs like PDF/A or PDF/UA. Triggers include convert to PDF, merge these PDFs, OCR this scan, extract tables, redact PII, sign this PDF, make this PDF/A, or linearize for web delivery.
tanstack-query
IncludedManage server state in React with TanStack Query v5. Covers useMutationState, simplified optimistic updates, throwOnError, network mode (offline/PWA), and infiniteQueryOptions. Use when setting up data fetching, fixing v4→v5 migration errors (object syntax, gcTime, isPending, keepPreviousData), or debugging SSR/hydration issues with streaming server components.
accelint-nextjs-best-practices
IncludedNext.js performance optimization and best practices. Use when writing Next.js code (App Router or Pages Router); implementing Server Components, Server Actions, or API routes; optimizing RSC serialization, data fetching, or server-side rendering; reviewing Next.js code for performance issues; fixing authentication in Server Actions; or implementing Suspense boundaries, parallel data fetching, or request deduplication.