Caching in Dev Mode #48481
Replies: 22 comments 19 replies
-
did this problem never arise anybody else or is my issue unclear 🤔? |
Beta Was this translation helpful? Give feedback.
-
Would also love a solution for this. Maybe a flag in the config to turn it on/off? |
Beta Was this translation helpful? Give feedback.
-
Looks like this topic hasn't received any attention from the next team yet, but I would also like a way to do this. |
Beta Was this translation helpful? Give feedback.
-
I'd like to have a solution too. In our case it's not solely about caching though, it's about trying to create a single worker thread to do specific work. Firing up multiple versions of that can cause serious issues. But if we can't create singletons, we can't reliably determine if there's only a single worker or not. |
Beta Was this translation helpful? Give feedback.
-
FWIW, I have created a custom memoization function that can be used to turn any other function to its cached variant: import { Cache } from "file-system-cache";
import crypto from "crypto";
const sharedCache = new Cache({ ttl: 60 * 5 });
type Producer<T> = (...args: any[]) => Promise<T>;
export function memoize<ReturnType, F extends Producer<ReturnType>>(
originalFunction: F,
key: string = originalFunction.name
): F {
if (!key) {
console.error("Cache key is empty, things will probably break.");
}
const memoized = async (...args: Parameters<F>): Promise<ReturnType> => {
const compoundKey = hashDigest([key, ...args]);
const cachedValue = await sharedCache.get(compoundKey);
if (cachedValue) {
return cachedValue;
} else {
const freshValue = await originalFunction(...args);
sharedCache.set(compoundKey, freshValue);
return freshValue;
}
};
return memoized as F;
}
function hashDigest(params: string[]): string {
const shasum = crypto.createHash("sha1");
shasum.update(params.join(":"));
return shasum.digest("hex").slice(0, 10);
} It’s used like this: const originalFunction = /* call some API or whatever */
const cached = memoize(originalFunction); // now use this instead of originalFunction I would still prefer some built-in solution in Next, though. |
Beta Was this translation helpful? Give feedback.
-
I'm in the same boat, using a Snowflake SDK client. Tried following workarounds like this Prisma doc would be totally acceptable but this seems to no longer work. Not sure if this is due to App Router vs Page Router differences or simply something changed in one of the bigger Next.js releases. Another use case, beyond cost (which I totally understand) is being rate limited. Assuming the reload does clear cache so that it doesn't have a memory leak, when dealing with connections it might still result in a client being blocked for abusing. The fewer differences between environments the better IMO. See also: |
Beta Was this translation helpful? Give feedback.
-
I have come across this as well, I can't really test the caching strategy in dev mode without somehow turning this on |
Beta Was this translation helpful? Give feedback.
-
Would also love this feature. I get 1000 API calls per day. |
Beta Was this translation helpful? Give feedback.
-
@leerob this would be a great feature when developing locally so we can test things. (also a DevTools for NextJS would be great, hint hint 😉 ) |
Beta Was this translation helpful? Give feedback.
-
This would be very helpful! |
Beta Was this translation helpful? Give feedback.
-
Throwing my hat in the ring that this would have saved a few hours of troubleshooting in my dev env. I want to test that the caching is working correctly, I shouldn't have to do that "in prod". |
Beta Was this translation helpful? Give feedback.
-
Is this possible in 14.1? Are there any other open related tickets or response from core devs? |
Beta Was this translation helpful? Give feedback.
-
This is really needed in development mode. |
Beta Was this translation helpful? Give feedback.
-
yep running into db timeout issues when adjusting my css... |
Beta Was this translation helpful? Give feedback.
-
We have the official Redis custom cache handler, but we cannot use it in the local environment. How can we verify the caching work? https://github.com/vercel/next.js/blob/canary/examples/cache-handler-redis/README.md |
Beta Was this translation helpful? Give feedback.
-
No, the issue is that we want to enable caching *in Dev mode, locally*. We
shouldn't have to deploy just to confirm caching is working, or blow up a
rate limited API while developing.
…On Fri, May 24, 2024, 2:14 AM Aaron Layton ***@***.***> wrote:
All caching in NextJS 15 is opt-in now, so this will probably solve your
issue
https://nextjs.org/blog/next-15-rc#caching-updates
—
Reply to this email directly, view it on GitHub
<#48481 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AALH4WI6F6Y6SEKG5Z7Q7KDZD3ZHZAVCNFSM6AAAAAAXBFHZBCVHI2DSMVQWIX3LMV43SRDJONRXK43TNFXW4Q3PNVWWK3TUHM4TKNBUGE2TM>
.
You are receiving this because you commented.Message ID: <vercel/next.
***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
I keep stumbling across this problem. It means that you can't develop your application the way it should actually be developed. If you want to use caching sensibly, you can't do this because you can not test and you can not benefit from it. For example, I don't want a token to be generated again and again to save the API. The normal cache would actually be ideal for this. Unfortunately, it doesn't work during development and so I have to find some stupid workarounds. Caching should ALWAYS work locally. We theoretically have native functions in the browser to skip the cache. Where possible, you should use them. For me, this (and this: #58736) has been one of the most important pain points when using Next.js because you write so much about caching and while I would love to use it properly, you make it so hard and annoying to actually implement it in a beneficial way. |
Beta Was this translation helpful? Give feedback.
-
I'm confused. I have also set up the Redis cache handler pattern as recommended in docs, but enabled it in dev because I need to build and dev against the cache. I see this message when starting next in dev mode:
However, the cache is populated and used everywhere we're using |
Beta Was this translation helpful? Give feedback.
-
I'd really appreciate this feature! |
Beta Was this translation helpful? Give feedback.
-
using |
Beta Was this translation helpful? Give feedback.
-
Is there a solution to this topic? |
Beta Was this translation helpful? Give feedback.
-
This feels..awkward |
Beta Was this translation helpful? Give feedback.
-
Summary
Hi, I would like to use built-in caching in
npm dev
mode. In my project I have a Server Component which is querying GPT API. The API response is slow and billed on a per request basis, so quering every time HMR kicks in is not a convenient solution. I can imagine that many similar situation exists, where requests are not "cheap" regarding time or money.For me, neither
app dir
revalidate
nor reactcache()
nor some custom runtime-cache would work during development (though perfectly once built for production).I found in https://stackoverflow.com/questions/69388338/nextjs-using-a-memory-cache-in-dev-does-not-work the hint to build a custom file based "cache" but was wondering if there is a more "idiomatic nextsjs" approach.
Is there anything I am missing? Any hints?
Additional information
Example
Beta Was this translation helpful? Give feedback.
All reactions