Skip to content

Refactor trpc.ts Middleware for Better Production Performance #2123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wolfcarves opened this issue May 13, 2025 · 0 comments
Open

Refactor trpc.ts Middleware for Better Production Performance #2123

wolfcarves opened this issue May 13, 2025 · 0 comments
Labels
🌟 enhancement New feature or request

Comments

@wolfcarves
Copy link

wolfcarves commented May 13, 2025

Is your feature request related to a problem? Please describe.

The current implementation in /server/api/trpc.ts applies artificial delays in both development and production, leading to unnecessary performance issues in production. This will improve performance in production environments while still providing the delay in development for simulating network latency.

Describe the solution you'd like to see

Refactor the timingMiddleware to only apply artificial delays during development (t._config.isDev) and not in production.

FROM

const timingMiddleware = t.middleware(async ({ next, path }) => {
  const start = Date.now();

  if (t._config.isDev) {
    // artificial delay in dev
    const waitMs = Math.floor(Math.random() * 400) + 100;
    await new Promise((resolve) => setTimeout(resolve, waitMs));
  }

  const result = await next();

  const end = Date.now();
  console.log(`[TRPC] ${path} took ${end - start}ms to execute`);

  return result;
});

export const publicProcedure = t.procedure.use(timingMiddleware);

TO

const timingMiddleware = t.middleware(async ({ next, path }) => {
    const start = Date.now();

    const waitMS = Math.floor(Math.random() * 400) + 100;
    await new Promise((resolve) => setTimeout(resolve, waitMS));

    const result = await next();

    const end = Date.now();
    console.log(`[TRPC] ${path} took ${end - start}ms to execute`);

    return result;
});

export const publicProcedure = t._config.isDev
    ? t.procedure.use(timingMiddleware)
    : t.procedure;

Describe alternate solutions

For a simplier approach

export const publicProcedure = t.procedure.use(t._config.isDev ? timingMiddleware : undefined);

Additional information

No response

@wolfcarves wolfcarves added the 🌟 enhancement New feature or request label May 13, 2025
@wolfcarves wolfcarves changed the title Refactor trpc.ts for better performance and environment-specific middleware Refactor trpc.ts Middleware for Better Production Performance May 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🌟 enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant