prevent redis attempting to connect during next build
Unanswered
West African Lion posted this in #help-forum
West African LionOP
I run into the problem where next build would attempt to connect to my redis during build, I only use my redis for ratelimit and do not want this behavior:
Code context:
version :
- next 15.0.3
- "@redis/client": "^1.6.0",
redis cilent :
rate-limit , i just call the cilent and import in the lua script:
Problem:
If i were to run npm run build I would get this inside my log
Code context:
version :
- next 15.0.3
- "@redis/client": "^1.6.0",
redis cilent :
import { env } from "@/env";
import { createClient } from "@redis/client";
const client = createClient({
url: env.REDIS_URL,
});
client.on("error", (err) => {
console.error("β Redis Client Error:", err);
});
client.on("connect", () => {
console.log("π Redis client attempting to connect...");
});
process.on("SIGINT", async () => {
console.log("π Shutting down Redis connection...");
await client.quit();
process.exit();
});
client.connect().then(() => {
console.log("β
Redis client connected");
});
export { client };
rate-limit , i just call the cilent and import in the lua script:
export class TokenBucket {
private storageKey: string;
public max: number;
public refillIntervalSeconds: number;
private ttlSeconds: number;
constructor(
storageKey: string,
max: number,
refillIntervalSeconds: number,
ttlSeconds = 86400,
) {
this.storageKey = storageKey;
this.max = max;
this.refillIntervalSeconds = refillIntervalSeconds;
this.ttlSeconds = ttlSeconds;
}
public async consume(key: string, cost: number): Promise<boolean> {
const result = (await client.evalSha(TOKEN_BUCKET_SHA, {
keys: [`${this.storageKey}:${key}`],
arguments: [
this.max.toString(),
this.refillIntervalSeconds.toString(),
cost.toString(),
Math.floor(Date.now() / 1000).toString(),
this.ttlSeconds.toString(),
],
})) as number[];
return Boolean(result[0]);
}
Problem:
If i were to run npm run build I would get this inside my log
3 Replies
West African LionOP
(conts):
log :
log :
ave@ave-ROG-Zephyrus-G14-GA401QM-GA401QM:~/Desktop/coding/emp$ npm run build
> emp@0.1.0 build
> next build
β² Next.js 15.0.3
- Environments: .env
Creating an optimized production build ...
β Compiled successfully
β Linting and checking validity of types
Collecting page data ..π Redis client attempting to connect...
π Redis client attempting to connect...
β
Redis client connected
β
Redis client connected
β Collecting page data
π Redis client attempting to connect...
β
Redis client connected
β Generating static pages (10/10)
π Shutting down Redis connection...
Finalizing page optimization . Collecting build traces .π Shutting down Redis connection...
π Shutting down Redis connection...
β Collecting build traces
β Finalizing page optimization
Route (app) Size First Load JS
β β /_not-found 896 B 101 kB
β Ζ /[locale] 165 B 100 kB
β Ζ /[locale]/[...rest] 165 B 100 kB
β Ζ /api/logout 165 B 100 kB
β Ζ /api/resend-otp 165 B 100 kB
β Ζ /api/user/events 165 B 100 kB
β Ζ /api/user/events/[eventId] 165 B 100 kB
β Ζ /api/user/events/[eventId]/event-reviews 165 B 100 kB
β Ζ /api/verify-otp 165 B 100 kB
+ First Load JS shared by all 100 kB
β chunks/4bd1b696-391fb2b781c9ada7.js 52.5 kB
β chunks/517-924557deb5874bc4.js 45.5 kB
β other shared chunks (total) 1.91 kB
Ζ Middleware 78.7 kB
β (Static) prerendered as static content
Ζ (Dynamic) server-rendered on demand
See how redis attempt a connection ,
wrapping the connect within the env I would get for example :
Would give me
wrapping the connect within the env I would get for example :
if (process.env.NEXT_PHASE !== "phase-production-build") {
client.connect().then(() => {
console.log("β
Redis client connected");
});
}
Would give me
ave@ave-ROG-Zephyrus-G14-GA401QM-GA401QM:~/Desktop/coding/emp$ npm run build
> emp@0.1.0 build
> next build
β² Next.js 15.0.3
- Environments: .env
Creating an optimized production build ...
β Compiled successfully
β Linting and checking validity of types
Collecting page data .Error: The client is closed
at w.S (/home/ave/Desktop/coding/emp/.next/server/chunks/354.js:1:27546)
at w.commandsExecutor (/home/ave/Desktop/coding/emp/.next/server/chunks/354.js:1:20157)
at e.<computed> [as scriptLoad] (/home/ave/Desktop/coding/emp/.next/server/chunks/354.js:1:66995)
at <unknown> (/home/ave/Desktop/coding/emp/.next/server/app/api/(auth)/verify-otp/route.js:117:15)
at t.a (/home/ave/Desktop/coding/emp/.next/server/webpack-runtime.js:1:891)
at 53003 (/home/ave/Desktop/coding/emp/.next/server/app/api/(auth)/verify-otp/route.js:1:4442)
at t (/home/ave/Desktop/coding/emp/.next/server/webpack-runtime.js:1:128)
at <unknown> (/home/ave/Desktop/coding/emp/.next/server/app/api/(auth)/verify-otp/route.js:117:174)
at t.a (/home/ave/Desktop/coding/emp/.next/server/webpack-runtime.js:1:891)
at 57894 (/home/ave/Desktop/coding/emp/.next/server/app/api/(auth)/verify-otp/route.js:117:119)
> Build error occurred
Error: Failed to collect page data for /api/verify-otp
at <unknown> (/home/ave/Desktop/coding/emp/node_modules/next/dist/build/utils.js:1234:15) {
type: 'Error'
}
My current solution is to mock the cilent like this during next build :
which would give me expected behavior but i am not sure if this the recommended way of doing it:
import { env } from "@/env";
import { createClient } from "@redis/client";
let client: ReturnType<typeof createClient> | null = null;
async function getClient() {
// Skip Redis connection during build
if (process.env.NEXT_PHASE === "phase-production-build") {
return {
isOpen: true,
evalSha: async () => [1],
scriptLoad: async () => "dummy-sha",
quit: async () => {},
} as unknown as ReturnType<typeof createClient>;
}
if (!client) {
client = createClient({
url: env.REDIS_URL,
});
client.on("error", (err) => {
console.error("β Redis Client Error:", err);
});
client.on("connect", () => {
console.log("π Redis client attempting to connect...");
});
}
if (!client.isOpen) {
await client.connect();
}
return client;
}
process.on("SIGINT", async () => {
if (client) {
console.log("π Shutting down Redis connection...");
await client.quit();
}
process.exit();
});
export { getClient };
which would give me expected behavior but i am not sure if this the recommended way of doing it:
ave@ave-ROG-Zephyrus-G14-GA401QM-GA401QM:~/Desktop/coding/emp$ npm run build
> emp@0.1.0 build
> next build
β² Next.js 15.0.3
- Environments: .env
Creating an optimized production build ...
β Compiled successfully
β Linting and checking validity of types
Collecting page data ..process.env.NEXT_PHASE phase-production-build
process.env.NEXT_PHASE phase-production-build
process.env.NEXT_PHASE phase-production-build
Collecting page data ...process.env.NEXT_PHASE phase-production-build
process.env.NEXT_PHASE phase-production-build
process.env.NEXT_PHASE phase-production-build
β Collecting page data
β Generating static pages (10/10)
β Collecting build traces
β Finalizing page optimization