最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

caching - How to configure Redis custom cache in Next.js 15? - Stack Overflow

programmeradmin0浏览0评论

I'm trying to set up Redis caching in Next.js 15, and I previously used @neshca/cache-handler in Next.js 14. However, it seems that @neshca/cache-handler does not support Next.js 15 yet, and I'm struggling to find a workaround.

What I Need

Redis-based caching for Next.js 15

A fallback LRU cache when Redis is unavailable

Compatibility with Next.js app directory (app/) and server components

What I Used Before (Next.js 14)

I used @neshca/cache-handler like this:

const { CacheHandler } = require('@neshca/cache-handler');
const createRedisHandler = require('@neshca/cache-handler/redis-stack').default;
const createLruHandler = require('@neshca/cache-handler/local-lru').default;
const { createClient } = require('redis');
const { PHASE_PRODUCTION_BUILD } = require('next/constants');
CacheHandler.onCreation(async () => {
    let client;
    if (PHASE_PRODUCTION_BUILD !== process.env.NEXT_PHASE) {
        try {
            client = createClient({ url: process.env.NEXT_PUBLIC_REDIS_URL });
            client.on('error', (e) => console.warn('Redis error', e));
        } catch (error) {
            console.warn('Failed to create Redis client:', error);
        }
    }
    if (client) {
        try {
            await client.connect();
            console.info('Redis client connected.');
        } catch (error) {
            console.warn('Failed to connect Redis client:', error);
            await client.disconnect();
        }
    }
    let redisHandler = client?.isReady
        ? await createRedisHandler({ client, keyPrefix: 'prefix:', timeoutMs: 1000 })
        : null;
    const LRUHandler = createLruHandler();
    return { handlers: [redisHandler, LRUHandler] };
});
module.exports = CacheHandler;

Problem with next.js 15:

@neshca/cache-handler doesn’t seem to support Next.js 15 yet as I'm getting this error:

TypeError: result.toUnchunkedString is not a function

I'm unsure how to replace it while keeping the same caching logic.

How should caching be handled in Next.js 15? Is there an alternative library that works similarly?

Any help would be greatly appreciated!

I previously used @neshca/cache-handler in Next.js 14, and it worked well for handling both Redis caching and fallback LRU caching.

Now, after upgrading to Next.js 15, I tried to use the same approach, but @neshca/cache-handler doesn't seem to be compatible. I expected it to work the same way as before, but it either fails to initialize or causes issues when used in the app/ directory with server components.

I also tried looking for an alternative caching solution that works seamlessly with Next.js 15, but I haven’t found anything that provides similar functionality.

I'm trying to set up Redis caching in Next.js 15, and I previously used @neshca/cache-handler in Next.js 14. However, it seems that @neshca/cache-handler does not support Next.js 15 yet, and I'm struggling to find a workaround.

What I Need

Redis-based caching for Next.js 15

A fallback LRU cache when Redis is unavailable

Compatibility with Next.js app directory (app/) and server components

What I Used Before (Next.js 14)

I used @neshca/cache-handler like this:

const { CacheHandler } = require('@neshca/cache-handler');
const createRedisHandler = require('@neshca/cache-handler/redis-stack').default;
const createLruHandler = require('@neshca/cache-handler/local-lru').default;
const { createClient } = require('redis');
const { PHASE_PRODUCTION_BUILD } = require('next/constants');
CacheHandler.onCreation(async () => {
    let client;
    if (PHASE_PRODUCTION_BUILD !== process.env.NEXT_PHASE) {
        try {
            client = createClient({ url: process.env.NEXT_PUBLIC_REDIS_URL });
            client.on('error', (e) => console.warn('Redis error', e));
        } catch (error) {
            console.warn('Failed to create Redis client:', error);
        }
    }
    if (client) {
        try {
            await client.connect();
            console.info('Redis client connected.');
        } catch (error) {
            console.warn('Failed to connect Redis client:', error);
            await client.disconnect();
        }
    }
    let redisHandler = client?.isReady
        ? await createRedisHandler({ client, keyPrefix: 'prefix:', timeoutMs: 1000 })
        : null;
    const LRUHandler = createLruHandler();
    return { handlers: [redisHandler, LRUHandler] };
});
module.exports = CacheHandler;

Problem with next.js 15:

@neshca/cache-handler doesn’t seem to support Next.js 15 yet as I'm getting this error:

TypeError: result.toUnchunkedString is not a function

I'm unsure how to replace it while keeping the same caching logic.

How should caching be handled in Next.js 15? Is there an alternative library that works similarly?

Any help would be greatly appreciated!

I previously used @neshca/cache-handler in Next.js 14, and it worked well for handling both Redis caching and fallback LRU caching.

Now, after upgrading to Next.js 15, I tried to use the same approach, but @neshca/cache-handler doesn't seem to be compatible. I expected it to work the same way as before, but it either fails to initialize or causes issues when used in the app/ directory with server components.

I also tried looking for an alternative caching solution that works seamlessly with Next.js 15, but I haven’t found anything that provides similar functionality.

Share Improve this question asked Mar 11 at 11:39 BesokBesok 1025 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

We are currently trying to get our own package up and running: https://github/trieb-work/nextjs-turbo-redis-cache it does support next.js 15 and works a bit different to the neshca idea. Take a look.

发布评论

评论列表(0)

  1. 暂无评论