Imported from mrjasonroy/cache-components-cache-handler (
AGENTS.md). Install upstream withnpx skills add mrjasonroy/cache-components-cache-handler. Copyright stays with the author.
AI Agent Instructions
Project Context
better-nextjs-cache-handler is a modern Next.js 16+ cache handler library built with:
- Turborepo - Monorepo management
- TypeScript - Strict mode, full type safety
- Biome - Linting and formatting
- Vitest + Playwright - Testing
- Docker Compose - Infrastructure
- pnpm - Package management
Project Goals
- Modern & Clean: Use latest best practices, minimal dependencies
- Next.js 16+ Focus: Full support for "use cache" directive and cache components
- Developer Experience: Easy to install, test, and contribute to
- Production Ready: Docker support, proper error handling, comprehensive tests
- AI-First: Automated testing, version checking, and release management
Code Standards
TypeScript
- Strict mode enabled - no
anytypes - Use type inference where possible
- Export types from public API
- JSDoc comments for public APIs
- Prefer interfaces over types for objects
Formatting (Biome)
- Do not manually format - Run
pnpm formatto fix all formatting - Import organization is automatic
- 100 character line width
- 2 space indentation
- Double quotes for strings
- Always use semicolons
- Trailing commas everywhere
Code Style
- Minimal & clean - Prefer composition over inheritance
- Self-documenting - Clear variable and function names
- No console.log - Use proper logging (we'll add later)
- Error handling - Always handle errors explicitly
- Async/await - Prefer over promises
Testing
- Write tests for all new features
- Unit tests in
*.test.tsfiles next to source - E2E tests in
tests/e2e/ - Aim for 80%+ coverage
Workflow
Before Making Changes
- Read
implementation/PROGRESS.mdfor current state - Check
implementation/NEXT_STEPS.mdfor planned work - Review
implementation/DECISIONS.mdfor context
While Making Changes
- Make your changes
- Run
pnpm lintto check for issues - Run
pnpm formatto fix formatting - Run
pnpm typecheckto verify types - Run
pnpm testto ensure tests pass
After Making Changes
- Update
implementation/PROGRESS.mdwith what you did - Update
implementation/NEXT_STEPS.mdwith what's next - Document important decisions in
implementation/DECISIONS.md - Add any issues to
implementation/ISSUES.md
Key Commands
# Development
pnpm dev # Start all apps in dev mode
pnpm build # Build all packages
pnpm test # Run all tests
pnpm test:e2e # Run Playwright tests
# Code Quality
pnpm lint # Check for linting issues
pnpm format # Format all code with Biome
pnpm typecheck # TypeScript type checking
# Docker
pnpm docker:up # Start Redis and services
pnpm docker:down # Stop all services
# Cleanup
pnpm clean # Remove all build artifacts
Next.js 16 Cache API
This project focuses on Next.js 16's new caching system:
- "use cache" directive - Component-level caching
- Cache Components - Full page caching
- Tag-based revalidation - Invalidate by tags
- Time-based revalidation - TTL support
- Build-time caching - Prime cache during build
Key Resources:
Package Structure
packages/
├── cache-handler/ # @mrjasonroy/better-nextjs-cache-handler
│ ├── src/
│ │ ├── types.ts # Core types
│ │ ├── handlers/
│ │ │ ├── memory.ts # Memory handler
│ │ │ └── composite.ts # Composite handler
│ │ └── index.ts # Public API
│ └── package.json
│
├── cache-handler-redis/ # @mrjasonroy/better-nextjs-cache-handler-redis
│ └── (coming soon)
│
└── cache-handler-elasticache/ # @mrjasonroy/better-nextjs-cache-handler-elasticache
└── (coming soon)
When Making Changes
Adding a New Feature
- Add types to
src/types.tsif needed - Implement in appropriate handler file
- Export from
src/index.ts - Write tests in
*.test.tsfile - Update package README if it's user-facing
Fixing a Bug
- Write a failing test first
- Fix the bug
- Verify test passes
- Document in
implementation/ISSUES.md
Updating Documentation
- Update relevant README files
- Update JSDoc comments
- Update examples if needed
Git Workflow
Pre-Commit Checklist (CRITICAL)
ALWAYS run these commands before EVERY commit:
pnpm lint # Check for linting issues
pnpm format # Fix all formatting (including JSON files)
pnpm typecheck # Verify TypeScript types
This is non-negotiable. CI will fail if formatting is incorrect, even for JSON files like package.json. Biome formats everything, not just TypeScript.
Commits
Use conventional commits:
feat: add Redis cluster supportfix: handle connection timeout correctlydocs: update installation guidetest: add e2e tests for revalidationchore: update dependencies
Branches
main- Production-ready code- Feature branches for development
- Merge via PR only
Common Patterns
Creating a Cache Handler
import type { CacheHandler, CacheHandlerContext, CacheValue } from "../types.js";
export class MyCacheHandler implements CacheHandler {
async get(key: string): Promise<CacheValue | null> {
// Implementation
}
async set(key: string, value: CacheValue, context?: CacheHandlerContext): Promise<void> {
// Implementation
}
async revalidateTag(tag: string): Promise<void> {
// Implementation
}
}
Error Handling
try {
await operation();
} catch (error) {
// Log error properly
throw new Error(`Failed to perform operation: ${error instanceof Error ? error.message : "Unknown error"}`);
}
Testing
import { describe, test, expect } from "vitest";
describe("MemoryCacheHandler", () => {
test("should store and retrieve values", async () => {
const handler = createMemoryCacheHandler();
const value = { kind: "FETCH", data: {...}, revalidate: false };
await handler.set("test-key", value);
const result = await handler.get("test-key");
expect(result).toEqual(value);
});
});
AI-Specific Instructions
When Analyzing Code
- Check
implementation/folder first for context - Read existing code before suggesting changes
- Follow established patterns
When Creating PRs
- Reference related issues
- Include test changes
- Update documentation
- Run all checks before submitting
When Finding Issues
- Document in
implementation/ISSUES.md - Include reproduction steps
- Suggest potential solutions
Questions?
If you're unsure about anything:
- Check existing code for patterns
- Review
implementation/DECISIONS.md - Look at test files for examples
- Ask the maintainer
Remember: Clean, minimal, well-tested code is the goal. Quality over quantity! 🎯