platforms icon indicating copy to clipboard operation
platforms copied to clipboard

refactor(middleware): improve readability by modularizing logic and adding documentation

Open joelybahh opened this issue 1 year ago • 4 comments

  • Broke down the middleware into smaller, more descriptive functions.
  • Added documentation to each function, explaining its purpose and usage.
  • Streamlined the code to enhance its readability, making the template more accessible to developers of varying skill levels.

joelybahh avatar Mar 30 '24 00:03 joelybahh

@joelybahh is attempting to deploy a commit to the Vercel Labs Team on Vercel.

A member of the Team first needs to authorize it.

vercel[bot] avatar Mar 30 '24 00:03 vercel[bot]

you are awesome

@joelybahh you are incredible! this refactor was so helpful to me.

could you help again?

I have launched this multi-tenant app successfully on vercel and I have several custom domains. It works locally great as well. My only issue is I cannot currently get it to work with production for cell generated domains or vercel generated preview domains.

joshualinog avatar Aug 29 '24 19:08 joshualinog

i believe the problem is going to be occurring here in the code:

/**
 * Processes the request hostname, adjusting it for local development and Vercel preview deployments.
 * It ensures that the hostname is standardized to use the application's root domain environment variable.
 *
 * @param {NextRequest} req - The incoming request object.
 * @returns {string} The processed hostname.
 */
function getProcessedHostname(req: NextRequest) {
  let hostname = req.headers.get("host")!.replace(".localhost:3000", `.${process.env.NEXT_PUBLIC_ROOT_DOMAIN}`);
  if (hostname.includes("---") && hostname.endsWith(`.${process.env.NEXT_PUBLIC_VERCEL_DEPLOYMENT_SUFFIX}`)) {
    hostname = `${hostname.split("---")[0]}.${process.env.NEXT_PUBLIC_ROOT_DOMAIN}`;
  }
  return hostname;
}

joshualinog avatar Aug 29 '24 19:08 joshualinog

i believe the problem is going to be occurring here in the code:

/**
 * Processes the request hostname, adjusting it for local development and Vercel preview deployments.
 * It ensures that the hostname is standardized to use the application's root domain environment variable.
 *
 * @param {NextRequest} req - The incoming request object.
 * @returns {string} The processed hostname.
 */
function getProcessedHostname(req: NextRequest) {
  let hostname = req.headers.get("host")!.replace(".localhost:3000", `.${process.env.NEXT_PUBLIC_ROOT_DOMAIN}`);
  if (hostname.includes("---") && hostname.endsWith(`.${process.env.NEXT_PUBLIC_VERCEL_DEPLOYMENT_SUFFIX}`)) {
    hostname = `${hostname.split("---")[0]}.${process.env.NEXT_PUBLIC_ROOT_DOMAIN}`;
  }
  return hostname;
}

I am actually wondering if vercel changed their construction of deployment suffixes... i have also upgraded to a paid account and wonder if maybe paid accounts have a different construction for deployment suffixes and hostname construction.

joshualinog avatar Aug 29 '24 19:08 joshualinog