export function isMainDomain(ctx?: NextPageContext): boolean { let hostname = ""; //…
- User
export function isMainDomain(ctx?: NextPageContext): boolean { let hostname = "";
// Server-side: get hostname from context if (ctx && ctx.req) { // ctx.req.headers.host can include port, so split it out hostname = (ctx.req.headers.host || "").split(":")[0]; } // Client-side: get hostname from window else if (typeof window !== "undefined") { hostname = window.location.hostname; }
if (!hostname) return false;
return hostname.includes(getEnv("MAIN_DOMAIN")) || hostname.endsWith(
.${getEnv("MAIN_DOMAIN")}); }-> optimize
- Assistant
Here's the optimized version of the
isMainDomainfunction. The optimizations include:- Corrected domain check: Replaced the
includesmethod with a more accurate check usinghostname === mainDomain || hostname.endsWith("." + mainDomain). This fixes potential false positives (e.g., "example.com.au" incorrectly matching "example.com") and ensures proper subdomain detection. - Case insensitivity: Converted both
hostnameandmainDomainto lowercase for case-insensitive comparison, as domain names are typically case-insensitive. - Single call to
getEnv: Moved thegetEnv("MAIN_DOMAIN")call to a single point at the top of the function to avoid redundant calls and improve efficiency. - Added null/empty checks: Explicitly checked for empty or invalid
mainDomainandhostnameto returnfalseearly, making the function more robust. - Code cleanliness: Improved readability by using consistent variable naming and early returns.
import { NextPageContext } from 'next'; import { getEnv } from './path-to-getEnv'; // Ensure this is imported correctly export function isMainDomain(ctx?: NextPageContext): boolean { const mainDomain = getEnv('MAIN_DOMAIN')?.toLowerCase(); if (!mainDomain) return false; // Early return if main domain is not set let hostname = ''; // Server-side: get hostname from context and remove port if (ctx && ctx.req) { const host = ctx.req.headers.host || ''; hostname = host.split(':')[0].toLowerCase(); } // Client-side: get hostname from window else if (typeof window !== 'undefined') { hostname = window.location.hostname.toLowerCase(); } if (!hostname) return false; // Early return if hostname is empty // Check for exact match or subdomain return hostname === mainDomain || hostname.endsWith(`.${mainDomain}`); }Key Improvements:
- Bug Fix: The original
includescheck could return true for invalid cases (e.g., "example.com.au" for main domain "example.com"). The new check is more precise and standard for subdomain detection. - Performance: By calling
getEnvonce and normalizing strings early, the function reduces unnecessary computations. - Robustness: Handles edge cases like undefined
ctx, missing headers, or unset environment variables more gracefully. - Readability: The code is more concise and easier to understand with early returns and clear logic.
This version should work seamlessly in a Next.js environment while being more reliable and efficient. If
getEnvis an expensive operation or could change dynamically, consider memoizing it at a higher level (e.g., using a singleton or React context) for further optimization. - Corrected domain check: Replaced the
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.