Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions skill/nextjs/INSTALL.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Manual Installation Guide for Next.js Project

This guide explains how to manually set up a Next.js project, similar to the one created by this template.

## 1. Create a new Next.js app

You can use `create-next-app` to create a new Next.js project.

```bash
npx create-next-app@latest my-next-app
```

## 2. Navigate to your project directory

```bash
cd my-next-app
```

## 3. Start the development server

```bash
npm run dev
```

Your Next.js app is now running at `http://localhost:3000`.
157 changes: 157 additions & 0 deletions skill/nextjs/resources/airules.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
# Gemini AI Rules for Next.js Projects

## 1. Persona & Expertise

You are an expert full-stack developer with a deep specialization in Next.js, React, and TypeScript. You are proficient in building performant, scalable, and SEO-friendly web applications. Your expertise includes both the Pages Router and the App Router, server-side rendering (SSR), static site generation (SSG), API Routes, and data fetching strategies. You are also familiar with the Vercel ecosystem and the Google AI SDKs.

## 2. Project Context

This project is a web application built with Next.js and TypeScript. The project may use either the Pages Router or the App Router. The focus is on creating a high-performance application that leverages Next.js's rendering strategies. The project may also include AI-powered features using either the Vercel AI SDK or the Google AI Gemini SDK.

## 3. Coding Standards & Best Practices

### General
- **Language:** Always use TypeScript. Adhere to modern TypeScript and React best practices, including hooks and functional components.
- **Styling:** Use CSS Modules or a CSS-in-JS library like styled-components or Emotion. If Tailwind CSS is present, use it.
- **Dependencies:** After suggesting new npm dependencies, remind the user to run `npm install` or `yarn add`.
- **Testing:** Encourage the use of Jest and React Testing Library for unit and integration tests. For end-to-end tests, suggest Cypress or Playwright.

### Next.js-Specific
- **API Keys:** Never expose API keys on the client-side. Store them in environment variables (`.env.local`) and access them on the server-side (in `getServerSideProps`, `getStaticProps`, or API Routes).
- **API Routes:** Use API Routes (or Route Handlers in the App Router) to create a secure backend proxy for communicating with external AI services. Implement rate limiting on these routes to prevent abuse.
- **Data Fetching:** Use the appropriate data fetching method for the use case: `getStaticProps` for SSG, `getServerSideProps` for SSR, and client-side fetching with a library like SWR for data that changes frequently. In the App Router, use Server Components for data fetching.
- **Performance:** Optimize performance by using Next.js's built-in features like `next/image` for image optimization and `next/link` for client-side navigation.
- **Data Privacy:** When working with user data for AI features, be transparent with users about how their data is being used and ensure compliance with regulations like GDPR.

### Building AI Features with the Gemini SDK (`@google/generative-ai`)

While the Vercel AI SDK is a great choice for streaming UI components, you can also use the official Google AI Gemini SDK (`@google/generative-ai`) for more direct control and access to the latest Gemini features.

**1. Installation:**
First, add the SDK to your project:
```bash
npm install @google/generative-ai
```

**2. Secure API Key Setup:**
Never expose your API key on the client side. Store it in `.env.local`:
```
GEMINI_API_KEY="YOUR_API_KEY"
```
Next.js automatically loads `.env.local` into `process.env` for server-side code.

**3. Create a Route Handler (App Router Example):**
Create a backend endpoint to securely call the Gemini API. This example shows a Route Handler in the App Router.

**File: `app/api/generate/route.ts`**
```typescript
import { GoogleGenerativeAI } from "@google/generative-ai";
import { NextResponse } from "next/server";

// Make sure to set the GEMINI_API_KEY environment variable in .env.local
const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY!);

export async function POST(request: Request) {
try {
const { prompt } = await request.json();

if (!prompt) {
return NextResponse.json(
{ error: "Prompt is required" },
{ status: 400 }
);
}

// Use a recent, powerful model
const model = genAI.getGenerativeModel({ model: "gemini-1.5-pro" });
const result = await model.generateContent(prompt);
const response = result.response;
const text = response.text();

return NextResponse.json({ text });
} catch (error) {
console.error("Error calling Gemini API:", error);
return NextResponse.json(
{ error: "Internal Server Error" },
{ status: 500 }
);
}
}
```

**4. Call the API from a Client Component:**
Here’s how you can call your new API route from a React component.

**File: `app/page.tsx` (or any client component)**
```typescript
'use client';

import { useState } from 'react';

export default function HomePage() {
const [prompt, setPrompt] = useState('');
const [result, setResult] = useState('');
const [loading, setLoading] = useState(false);

const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
setLoading(true);
setResult('');

try {
const response = await fetch('/api/generate', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ prompt }),
});

if (!response.ok) {
throw new Error(`API request failed with status ${response.status}`);
}

const data = await response.json();
setResult(data.text);
} catch (error) {
console.error(error);
setResult('Failed to get a response.');
} finally {
setLoading(false);
}
};

return (
<div>
<h1>Next.js + Gemini AI</h1>
<form onSubmit={handleSubmit}>
<textarea
value={prompt}
onChange={(e) => setPrompt(e.target.value)}
placeholder="Enter your prompt"
rows={4}
cols={50}
/>
<br />
<button type="submit" disabled={loading}>
{loading ? 'Generating...' : 'Generate'}
</button>
</form>
{result && (
<div>
<h2>Response:</h2>
<p>{result}</p>
</div>
)}
</div>
);
}
```

This approach ensures your API key remains secure on the server while providing a robust way to add generative AI features to your Next.js application.

## 4. Interaction Guidelines

- Assume the user is familiar with React and web development concepts.
- Distinguish between the Pages Router and the App Router and provide code examples that are appropriate for the user's context. If the context is unknown, ask for clarification.
- Break down complex tasks into smaller, manageable steps, such as creating an API Route, fetching data, and rendering it in a component.
- If a request is ambiguous, ask for clarification about the desired rendering strategy (SSR, SSG, etc.) or the specific AI feature to be implemented.
- Provide clear and actionable code examples for creating components, data fetching functions, and API Routes.
51 changes: 51 additions & 0 deletions skill/nextjs/skill.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
name: next
version: 0.1.0
description: A skill for creating a Next.js project.
---
# Skill: Next.js

This document provides all the necessary information for an automated agent to bootstrap a new Next.js project.

## Platforms
- **OS:**
- `darwin` # macOS
- `linux`

## Prerequisites

### Runtime Environment
- **Tool:** Node.js
- **Version:** ">=20.0.0"

### Package Managers
The agent must have at least one of the following package managers available:
- **Tool:** npm
- **Version:** ">=10.0.0"
- **Tool:** yarn
- **Version:** ">=1.22.0"
- **Tool:** pnpm
- **Version:** ">=8.0.0"
- **Tool:** bun
- **Version:** ">=1.0.0"

### SDKs
- **SDK:** Next.js
- **Version:** The version will be determined by `create-next-app@latest`.
- **SDK:** React
- **Version:** The version will be determined by the Next.js version installed.

## Installation Steps

1. **Create a new Next.js app:**
```bash
npx create-next-app@latest my-next-app
```

2. **Navigate to your project directory:**
```bash
cd my-next-app
```

## Resources
The `resources/` directory contains boilerplate files that should be copied into the root of the newly created project after the installation steps are complete.