How to test if my caching is working?
Unanswered
American black bear posted this in #help-forum
American black bearOP
How can I test if the functions are being cached correctly with the
For example I have something like this:
"use cache"
directive? The docs say that by default functions cached with use cache, without specifying cacheLife
last for 5 minutes, but when I change something in my CMS and refresh the page displaying the content new theme has been applied even though it should last those 5 minutes if I don't revalidate it. Is this expected or am I not caching my function output properly?For example I have something like this:
export async function getTheme({
isDraftMode = false,
tenantDomain,
}: {
isDraftMode?: boolean;
tenantDomain: string | null;
}): Promise<ThemeType | null> {
"use cache";
cacheTag(
settingsTag.global,
settingsTag.tenant(tenantDomain),
settingsTag.theme(tenantDomain),
);
try {
const db = await dbClient()
const result = // ... fetch logic
return result ?? null
} catch (error) {
console.error(error);
return null;
}
}
13 Replies
American black bearOP
Yeah I was trying in the production
American black bearOP
This is how I am currently handling draft mode for preview. Could this be causing an issue with caching?
import { headers, draftMode as nextDraftMode } from "next/headers";
export const dynamic = "force-dynamic";
export default async function DynamicPage({
params,
searchParams,
}: {
params: DynamicPageParams;
searchParams: DynamicPageSearchParams;
}) {
let page: Page | null = null;
const { slug } = await params;
const { draft } = await searchParams;
const draftMode = await nextDraftMode();
const tenantDomain = await getTenantDomain();
try {
let user: User | null = null;
if (draft === "true") {
const reqHeaders = await headers();
user = await getUser(reqHeaders);
}
if (user?.id) {
draftMode.enable();
} else {
draftMode.disable();
}
page = await getPageBySlug({
slug,
isDraftMode: draftMode.isEnabled,
tenantDomain,
});
} catch (error) {
console.error(error);
}
if (!page) {
return notFound();
}
return (
<>
{draftMode.isEnabled && <RefreshRouteOnSave />}
<main className="min-h-[calc(100vh-73px)]">
{page.content?.map((content) => (
<SectionRenderer key={content.id} content={content} />
))}
</main>
</>
);
}
and keep in mind use cache is currently in canary, so there can be bugs
American black bearOP
// next.config.json
import { withPayload } from "@payloadcms/next/withPayload";
/**
* Run `build` or `dev` with `SKIP_ENV_VALIDATION` to skip env validation. This is especially useful
* for Docker builds.
*/
import "./src/env.js";
import { env } from "./src/env.js";
/** @type {import("next").NextConfig} */
const config = {
experimental: {
useCache: true,
},
images: {
remotePatterns: [
{
hostname: "app",
protocol: "http",
port: "3000",
},
{
hostname: "localhost",
protocol: "http",
port: "3000",
},
{
hostname: env.NEXT_PUBLIC_DOMAIN,
protocol: "https",
},
],
},
async rewrites() {
const rewrites = [];
if (env.NEXT_PUBLIC_ENABLE_ANALYTICS === "true") {
rewrites.push(
{
source: "/ingest/static/:path*",
destination: `${env.NEXT_PUBLIC_POSTHOG_ASSETS_HOST}/static/:path*`,
},
{
source: "/ingest/:path*",
destination: `${env.NEXT_PUBLIC_POSTHOG_HOST}/:path*`,
},
{
source: "/ingest/decide",
destination: `${env.NEXT_PUBLIC_POSTHOG_HOST}/decide`,
},
);
}
return rewrites;
},
skipTrailingSlashRedirect: true,
output: "standalone",
transpilePackages: ["@t3-oss/env-nextjs", "@t3-oss/env-core"],
};
export default withPayload(config);
// package.json
dependencies: {
// ...
"next": "15.3.3",
}
American black bearOP
any ideas or tips on how I can debug this?
American black bearOP
Is that mandatory?
I think the docs say that default is 15min if no cacheLife is specified
American black bearOP
@American black bear Is that mandatory?
It could be because you've created a cacheTag, and you haven't revalidated it anywhere so there is basically no cache