Skip to content
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

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

joelybahh
Copy link

  • 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.

Copy link

vercel bot commented Mar 30, 2024

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

A member of the Team first needs to authorize it.

@joshualinog
Copy link

joshualinog commented Aug 29, 2024

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
Copy link

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
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants