This example application demonstrates various Next.js caching functionalities using the Redis cache handler. It provides a comprehensive UI to explore and test different caching strategies.
First, install dependencies:
npm iNext.js does not use the cache handler in development mode. This is a Next.js limitation - caching is intentionally disabled in dev mode for faster hot reloading and to ensure developers always see fresh data.
To test caching functionality, you must use production mode:
npm run build
npm run startFor development (without caching):
npm run devOpen http://localhost:3000 with your browser to see the result.
Modify the .env file if you need to configure Redis connection settings. The default Redis URL is used if not specified.
The example supports both Redis clients:
- @redis/client (default): Set
REDIS_TYPE="redis"or leave unset - ioredis: Set
REDIS_TYPE="ioredis"
This allows you to test the ioredisAdapter functionality.
The application includes several examples demonstrating different Next.js caching features:
Overview page listing all available examples with descriptions and features.
Demonstrates the default fetch caching behavior with force-cache.
Features:
- Default caching behavior (indefinite cache duration)
- Perfect for static or rarely-changing data
- Shows how Next.js caches by default
Try it:
- Visit
/examples/default-cacheto see cached data - The timestamp will remain the same on subsequent requests
- Data is cached until manually cleared or build is redeployed
Shows fetch with no-store option, which always fetches fresh data.
Features:
- Never caches responses
- Always fetches fresh data from API
- Perfect for real-time or user-specific data
- Timestamp changes on every request
Try it:
- Visit
/examples/no-storeto see fresh data on every load - Refresh the page multiple times - timestamp changes each time
- Compare with other caching strategies
Shows fetch with time-based revalidation (standalone, without tags).
Features:
- Automatic revalidation after specified time (30 seconds)
- Balances freshness with performance
- Standalone example of
next.revalidate
Try it:
- Visit
/examples/time-based-revalidationto see cached data - Refresh within 30 seconds - timestamp stays the same
- Wait 30+ seconds and refresh - timestamp updates
Demonstrates fetch caching with tags and time-based revalidation.
Features:
- Time-based revalidation (24 hours)
- Cache tags for selective invalidation
- Clear cache button to test tag revalidation
- Shows character data from Futurama API
- Displays cache information and rendered timestamp
Try it:
- Visit
/examples/fetch-tagsto see cached data - Click "Clear Cache" to invalidate the cache
- Reload the page to see fresh data
Demonstrates persistent caching with unstable_cache for function results.
Features:
- Cache any function, not just fetch requests
- Tags and revalidation support
- Side-by-side comparison with fetch caching
- Perfect for database queries and computations
- Shows when to use unstable_cache vs fetch
Try it:
- Visit
/examples/unstable-cacheto see both caching methods - Compare the timestamps and behavior
- Click "Clear Tag Cache" to invalidate both caches
- Understand when to use unstable_cache vs fetch
Demonstrates the updated revalidateTag() API in Next.js 16 with cacheLife profiles.
Features:
- Breaking change from Next.js 15 (cacheLife now required)
- Different cacheLife profiles: 'max', 'hours', 'days'
- Stale-while-revalidate behavior
- Examples for each profile type
- Code examples showing migration from Next.js 15
Try it:
- Visit
/examples/revalidate-tag-cachelifeto see all three profiles - Click "Revalidate" buttons to test each profile
- Compare the behavior of different cacheLife profiles
- See code examples for Next.js 15 vs Next.js 16
Incremental Static Regeneration with generateStaticParams.
Features:
- Static generation at build time
- On-demand regeneration
- Time-based revalidation (1 hour)
- Multiple blog post routes
Try it:
- Visit
/examples/isr/blog/1for the first post - Try different IDs like
/examples/isr/blog/2,/examples/isr/blog/3 - Check the rendered timestamp to see caching in action
Tests static params generation with dynamic routes.
Features:
- Static params generation
- Dynamic params support
- Short revalidation period (5 seconds) for testing
- Shows generation type (static vs dynamic)
Try it:
- Visit
/examples/static-params/cache(pre-generated) - Try
/examples/static-params/test1or/examples/static-params/test2(on-demand)
Unified endpoint for revalidating cache by tag or path.
Usage:
Tag-based revalidation (GET):
GET /api/revalidate?tag=futurama- Revalidates cache for the "futurama" tag with 'max' profileGET /api/revalidate?tag=futurama&cacheLife=hours- Revalidates with 'hours' profileGET /api/revalidate?tag=futurama&cacheLife=days- Revalidates with 'days' profile
Tag-based revalidation (POST):
POST /api/revalidatewith body{ "tag": "futurama" }- Revalidates cache for a tag (defaults to 'max')POST /api/revalidatewith body{ "tag": "futurama", "cacheLife": "hours" }- Revalidates with specific profile
Path-based revalidation (POST):
POST /api/revalidatewith body{ "path": "/examples/default-cache" }- Revalidates cache for a specific path
Examples:
# Revalidate by tag (GET) - defaults to 'max' profile
curl http://localhost:3000/api/revalidate?tag=futurama
# Revalidate by tag with specific profile (GET)
curl http://localhost:3000/api/revalidate?tag=futurama&cacheLife=hours
# Revalidate by tag (POST) - defaults to 'max' profile
curl -X POST http://localhost:3000/api/revalidate \
-H "Content-Type: application/json" \
-d '{"tag": "futurama"}'
# Revalidate by tag with specific profile (POST)
curl -X POST http://localhost:3000/api/revalidate \
-H "Content-Type: application/json" \
-d '{"tag": "futurama", "cacheLife": "days"}'
# Revalidate by path (POST)
curl -X POST http://localhost:3000/api/revalidate \
-H "Content-Type: application/json" \
-d '{"path": "/examples/default-cache"}'This example uses a custom Redis cache handler configured in cache-handler.mjs. The handler supports:
- Redis string-based caching
- Local LRU fallback
- Composite caching strategy
- Tag-based cache invalidation
Note: The cache handler only works in production mode. In development mode, Next.js bypasses the cache handler entirely. You'll see a warning message in the console: "Next.js does not use the cache in development mode. Use production mode to enable caching."
- Next.js 16
- React 19
- TypeScript
- Tailwind CSS
- Redis
- @fortedigital/nextjs-cache-handler