本文へ移動
cccskills
無料GitHub で公開

nextjs-typescript

Provides expert knowledge for building Next.js 15+ applications with the App Router, TypeScript, Server Components, Server Actions, and TanStack Query.

インストール方法を見る

含まれるファイル(1)

  • SKILL.md9.4 KB

SKILL.md(原文)

インストールする前に、エージェントに与えられる指示の中身を確認できます。

Next.js + TypeScript

Server vs Client Components

The App Router defaults to Server Components. Every component is a Server Component unless it explicitly opts in to the client.

Decision Rule

Does the component need any of the following?
  - useState / useReducer
  - useEffect / lifecycle methods
  - Browser APIs (window, document, localStorage)
  - Event listeners (onClick, onChange, onSubmit)
  - Third-party libraries that require the DOM

  YES → Client Component (`"use client"` directive)
  NO  → Server Component (default, no directive needed)

Keep the "use client" boundary as far down the component tree as possible. Wrap only the interactive leaf node, not the entire page.

Server Component (default)

// app/users/page.tsx — no directive needed
import { db } from '@/lib/db';

export default async function UsersPage() {
  // Direct database access — no API round-trip needed
  const users = await db.user.findMany({ orderBy: { createdAt: 'desc' } });

  return (
    <ul>
      {users.map(user => (
        <li key={user.id}>{user.email}</li>
      ))}
    </ul>
  );
}

Client Component

// components/ui/counter.tsx
'use client';

import { useState } from 'react';

interface Props {
  initialCount?: number;
}

export function Counter({ initialCount = 0 }: Props) {
  const [count, setCount] = useState(initialCount);
  return <button onClick={() => setCount(c => c + 1)}>Count: {count}</button>;
}

Composing Server and Client

// app/dashboard/page.tsx — Server Component
import { Counter } from '@/components/ui/counter';  // Client
import { getUser } from '@/lib/queries';            // Server-only function

export default async function DashboardPage() {
  const user = await getUser();  // runs on the server

  return (
    <div>
      <h1>Welcome, {user.name}</h1>
      <Counter initialCount={user.loginCount} />  {/* client island */}
    </div>
  );
}

Passing server data to client components as props is correct. The server renders the Server Component tree first, serializes the props, and sends them to the client.

Data Fetching

Server Component Fetching (recommended for initial data)

// app/posts/[id]/page.tsx
interface Props {
  params: Promise<{ id: string }>;
}

export default async function PostPage({ params }: Props) {
  const { id } = await params;
  const post = await fetch(`https://api.example.com/posts/${id}`, {
    next: { revalidate: 3600 },  // ISR: revalidate every hour
  }).then(r => r.json());

  if (!post) notFound();

  return <article>{post.title}</article>;
}

Cache strategies:

cache optionBehavior
force-cacheCache indefinitely (default for fetch in RSC)
no-storeNever cache — fresh on every request
next: { revalidate: N }ISR — revalidate after N seconds
next: { tags: ['posts'] }On-demand revalidation via tag

Server Actions (mutations)

Server Actions run on the server. Use them for form submissions and mutations. Never use them for reads.

// app/posts/create/actions.ts
'use server';

import { revalidatePath } from 'next/cache';
import { redirect } from 'next/navigation';
import { z } from 'zod';
import { db } from '@/lib/db';

const CreatePostSchema = z.object({
  title: z.string().min(1).max(200),
  content: z.string().min(1),
});

export async function createPost(formData: FormData) {
  const parsed = CreatePostSchema.safeParse({
    title: formData.get('title'),
    content: formData.get('content'),
  });

  if (!parsed.success) {
    return { error: parsed.error.flatten().fieldErrors };
  }

  await db.post.create({ data: parsed.data });

  revalidatePath('/posts');
  redirect('/posts');
}
// app/posts/create/page.tsx
import { createPost } from './actions';

export default function CreatePostPage() {
  return (
    <form action={createPost}>
      <input name="title" type="text" required />
      <textarea name="content" required />
      <button type="submit">Create</button>
    </form>
  );
}

Rules for Server Actions:

  • Always validate input with Zod — do not trust FormData values
  • Return error objects for validation failures; use redirect() for success
  • Call revalidatePath() or revalidateTag() after mutations that affect cached data
  • Never put secrets or auth logic in Client Components — keep it in actions

TanStack Query (client-side data)

Use TanStack Query for data that must stay fresh on the client: real-time feeds, user-specific data after mutations, optimistic updates.

// components/posts/post-list.tsx
'use client';

import { useQuery } from '@tanstack/react-query';

interface Post {
  id: string;
  title: string;
}

export function PostList() {
  const { data, isLoading, error } = useQuery<Post[]>({
    queryKey: ['posts'],
    queryFn: () => fetch('/api/posts').then(r => r.json()),
    staleTime: 60_000,   // treat data as fresh for 60 seconds
  });

  if (isLoading) return <p>Loading...</p>;
  if (error) return <p>Failed to load posts.</p>;

  return (
    <ul>
      {data?.map(post => <li key={post.id}>{post.title}</li>)}
    </ul>
  );
}

Do not use TanStack Query for data that a Server Component can fetch directly. The extra client-side fetch is unnecessary when the data can be fetched at render time on the server.

Route Handlers

Use route handlers for: webhooks, third-party OAuth callbacks, or endpoints consumed by non-Next.js clients.

// app/api/posts/route.ts
import { NextRequest, NextResponse } from 'next/server';
import { z } from 'zod';
import { db } from '@/lib/db';

const CreatePostBody = z.object({
  title: z.string().min(1),
  content: z.string().min(1),
});

export async function GET() {
  const posts = await db.post.findMany({ orderBy: { createdAt: 'desc' } });
  return NextResponse.json(posts);
}

export async function POST(request: NextRequest) {
  const body = await request.json();
  const parsed = CreatePostBody.safeParse(body);

  if (!parsed.success) {
    return NextResponse.json(
      { error: parsed.error.flatten() },
      { status: 400 }
    );
  }

  const post = await db.post.create({ data: parsed.data });
  return NextResponse.json(post, { status: 201 });
}

Metadata API

// app/posts/[id]/page.tsx
import type { Metadata } from 'next';

interface Props {
  params: Promise<{ id: string }>;
}

export async function generateMetadata({ params }: Props): Promise<Metadata> {
  const { id } = await params;
  const post = await fetch(`/api/posts/${id}`).then(r => r.json());

  return {
    title: post.title,
    description: post.excerpt,
    openGraph: {
      title: post.title,
      images: [{ url: post.coverImage }],
    },
  };
}

Never hardcode metadata in <head> tags — use the Metadata API. It handles deduplication, inheritance, and streaming correctly.

Streaming with Suspense

// app/dashboard/page.tsx
import { Suspense } from 'react';
import { UserStats } from './user-stats';   // slow data fetch
import { RecentActivity } from './recent'; // fast data fetch

export default function DashboardPage() {
  return (
    <div>
      <RecentActivity />   {/* renders immediately */}
      <Suspense fallback={<p>Loading stats...</p>}>
        <UserStats />      {/* streams in when ready */}
      </Suspense>
    </div>
  );
}

Wrap independently slow data sources in their own <Suspense> boundaries. Do not wrap the entire page — that defeats streaming.

TypeScript Conventions

// Use type imports for type-only imports
import type { User } from '@prisma/client';

// Prefer interfaces for object shapes that may be extended
interface UserCardProps {
  user: Pick<User, 'id' | 'name' | 'email'>;
  onSelect?: (id: string) => void;
}

// Use type aliases for unions and computed types
type Status = 'active' | 'inactive' | 'pending';
type UserWithPosts = User & { posts: Post[] };

Rules:

  • Never use any — use unknown and narrow with type guards
  • Use satisfies to validate objects against a type without widening
  • Co-locate type definitions with the component or function that uses them; export only what other modules need
  • Use next/navigation hooks (useRouter, usePathname) not next/router — the latter is Pages Router only

Project Structure

app/
  (auth)/                — route group, no URL segment
    login/page.tsx
    register/page.tsx
  (dashboard)/
    dashboard/page.tsx
    posts/
      [id]/page.tsx
      create/
        page.tsx
        actions.ts
  api/
    posts/route.ts
  layout.tsx             — root layout
  not-found.tsx

components/
  ui/                    — generic, reusable components
  posts/                 — domain-specific components

lib/
  db.ts                  — database client
  auth.ts                — auth configuration
  queries.ts             — reusable server-side query functions

レビュー

まだレビューはありません。使ってみた感想をお寄せください。

同じリポジトリのスキル

概要と使いどころ

Use when adding monitoring, metrics, logging, or tracing to Spring AI or LLM integration code. Covers token tracking, latency measurement, cost estimation, and prompt/response logging. Use when user mentions AI monitoring, token costs, or LLM observability.

日本語の概要は準備中です。原文の説明を表示しています。

lgzarturo/springboot-course32026年10月12日 更新

android

無料

Provides expert knowledge for building modern native Android apps using Kotlin, Jetpack Compose, Material Design 3, MVI architecture, Hilt dependency injection, and Media3/ExoPlayer.

日本語の概要は準備中です。原文の説明を表示しています。

lgzarturo/springboot-course32026年10月12日 更新

Design, implement, review, and audit Android Jetpack Compose UI/UX for phone interfaces. Use when a request concerns concrete Compose layout, components, interaction, accessibility, or visual behavior. Do not activate for framework or dependency presence alone; non-UI Android work is excluded.

日本語の概要は準備中です。原文の説明を表示しています。

lgzarturo/springboot-course32026年10月12日 更新

Provides expert knowledge for designing, implementing, and managing REST API versioning strategies with deprecation workflows.

日本語の概要は準備中です。原文の説明を表示しています。

lgzarturo/springboot-course32026年10月12日 更新

astro

無料

Provides expert knowledge for building Astro 5+ sites with Islands Architecture, Content Collections, TypeScript, and performance-first rendering strategies.

日本語の概要は準備中です。原文の説明を表示しています。

lgzarturo/springboot-course32026年10月12日 更新

Audits token storage mechanisms to prevent XSS-based JWT theft.

日本語の概要は準備中です。原文の説明を表示しています。

lgzarturo/springboot-course32026年10月12日 更新

lgzarturo のスキルをすべて見る

このスキルの問題を報告する