- Remove .prettierrc (dead config, Biome replaces Prettier) - Rename lighthouserc.json to lighthouserc.cjs with module.exports for ESM compat - Add logger.warn to empty catch blocks in auth, register, site-settings, prisma-cache, redis, security, rate-limit - Unify ActionResult type: action-helper.ts uses 'ok' consistent with safe-action-shared.ts - Add noUnusedLocals + noUnusedParameters to tsconfig + fix 25 pre-existing unused vars - Replace barrel export src/types/index.ts with direct @/types/common imports - Make trustHost conditional (development only) in auth.ts - Add pre-flight URL validation to update-Nitrov3.sh to catch image.library.url misconfigurations - Improve NITRO_IMAGE_LIBRARY_URL content validation in pre-flight & post-compute checks
47 lines
1.3 KiB
TypeScript
47 lines
1.3 KiB
TypeScript
import "server-only";
|
|
|
|
import Redis from "ioredis";
|
|
import { env } from "@/env";
|
|
import { logger } from "@/lib/logger";
|
|
|
|
const globalForRedis = globalThis as unknown as {
|
|
redis?: Redis | null;
|
|
redisMissingWarned?: boolean;
|
|
};
|
|
|
|
function createRedis(): Redis | null {
|
|
const url = env.REDIS_URL;
|
|
if (!url) {
|
|
if (env.NODE_ENV === "production" && !globalForRedis.redisMissingWarned) {
|
|
globalForRedis.redisMissingWarned = true;
|
|
logger.error(
|
|
"[redis] REDIS_URL is unset in production. Rate limits, site-settings cache, and JWT session invalidation fall back to in-process memory and will not work correctly across multiple instances or restarts. Set REDIS_URL in .env.",
|
|
);
|
|
}
|
|
return null;
|
|
}
|
|
try {
|
|
const client = new Redis(url, {
|
|
maxRetriesPerRequest: 3,
|
|
retryStrategy(times) {
|
|
if (times > 3) return null;
|
|
return Math.min(times * 200, 2000);
|
|
},
|
|
lazyConnect: true,
|
|
});
|
|
client.on("error", (err) => {
|
|
logger.warn("[redis] Connection error", { error: String(err) });
|
|
});
|
|
return client;
|
|
} catch (err) {
|
|
logger.warn("[redis] Failed to create Redis client", {
|
|
error: String(err),
|
|
});
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export const redis: Redis | null =
|
|
globalForRedis.redis !== undefined ? globalForRedis.redis : createRedis();
|
|
if (globalForRedis.redis === undefined) globalForRedis.redis = redis;
|