@trieb.work/nextjs-turbo-redis-cache
Version:
Designed for speed, scalability, and optimized performance, nextjs-turbo-redis-cache is your custom cache handler for demanding production environments.
45 lines (40 loc) • 1.25 kB
text/typescript
import fs from 'node:fs';
import path from 'node:path';
export function readBuildId(serverDistDir?: string): string | undefined {
try {
if (serverDistDir) {
const buildIdPath = path.join(serverDistDir, '..', 'BUILD_ID');
const buildId = fs.readFileSync(buildIdPath, 'utf8').trim();
return buildId || undefined;
}
} catch {
// fall through to cwd-based read
}
try {
const fromCwd = path.join(process.cwd(), '.next', 'BUILD_ID');
const buildId = fs.readFileSync(fromCwd, 'utf8').trim();
return buildId || undefined;
} catch {
return undefined;
}
}
export function resolveKeyPrefix({
optionKeyPrefix,
serverDistDir,
env,
}: {
optionKeyPrefix?: string;
serverDistDir?: string;
env: NodeJS.ProcessEnv;
}): string {
// If the option is explicitly provided, honor it even if it's an empty string
if (optionKeyPrefix !== undefined) {
return optionKeyPrefix;
}
const keyPrefixEnv =
env.KEY_PREFIX && env.KEY_PREFIX.length > 0 ? env.KEY_PREFIX : undefined;
const vercelUrl =
env.VERCEL_URL && env.VERCEL_URL.length > 0 ? env.VERCEL_URL : undefined;
const buildId = readBuildId(serverDistDir);
return keyPrefixEnv ?? vercelUrl ?? buildId ?? 'UNDEFINED_URL_';
}