import type { RouterConfig } from "@nuxt/schema";
import { useRuntimeConfig } from "#app";
import { useRequestHeaders } from "nuxt/app";
function getSubdomain(host: string) {
const h = host?.split(':')[0].toLowerCase();
const parts = h.split('.');
return parts.length > 2 ? parts[0] : null;
}
function isSubdomainRequest(): boolean {
if (process.server) {
const headers = useRequestHeaders(['host', 'x-forwarded-host']);
const host = headers['x-forwarded-host'] || headers.host || '';
const sub = getSubdomain(host);
return sub && !['www', 'stage'].includes(sub);
}
if (process.client) {
const sub = getSubdomain(location.hostname);
return sub && !['www', 'stage'].includes(sub);
}
return false;
}
function transformRoutes(routes: any[], isSub: boolean, shared: string[]) {
const isSubRoute = (r: any) => r.path.startsWith("/subdomain");
const isShared = (r: any) => shared.some(p => r.path === p || r.path.startsWith(p + "/"));
if (isSub) {
const sharedRoutes = routes.filter(isShared);
const subRoutes = routes.filter(isSubRoute).map(r => ({
...r,
path: r.path.replace("/subdomain", ""),
name: r.name?.replace("subdomain-", "") || "index",
}));
return [...sharedRoutes, ...subRoutes];
}
return routes.filter(r => !isSubRoute(r));
}
export default <RouterConfig>{
routes: (_routes) => {
const { ssrContext } = useNuxtApp();
const config = useRuntimeConfig();
const isSub = isSubdomainRequest();
return transformRoutes(_routes, isSub, ["/about"]);
},
};
import type { RouterConfig } from "@nuxt/schema";
import { useRuntimeConfig } from "#app";
import { useRequestHeaders } from "nuxt/app";
function getSubdomain(host: string) {
const h = host?.split(':')[0].toLowerCase();
const parts = h.split('.');
return parts.length > 2 ? parts[0] : null;
}
function isSubdomainRequest(): boolean {
if (process.server) {
const headers = useRequestHeaders(['host', 'x-forwarded-host']);
const host = headers['x-forwarded-host'] || headers.host || '';
const sub = getSubdomain(host);
return sub && !['www', 'stage'].includes(sub);
}
if (process.client) {
const sub = getSubdomain(location.hostname);
return sub && !['www', 'stage'].includes(sub);
}
return false;
}
function transformRoutes(routes: any[], isSub: boolean, shared: string[]) {
const isSubRoute = (r: any) => r.path.startsWith("/subdomain");
const isShared = (r: any) => shared.some(p => r.path === p || r.path.startsWith(p + "/"));
if (isSub) {
const sharedRoutes = routes.filter(isShared);
const subRoutes = routes.filter(isSubRoute).map(r => ({
...r,
path: r.path.replace("/subdomain", ""),
name: r.name?.replace("subdomain-", "") || "index",
}));
return [...sharedRoutes, ...subRoutes];
}
return routes.filter(r => !isSubRoute(r));
}
export default <RouterConfig>{
routes: (_routes) => {
const { ssrContext } = useNuxtApp();
const config = useRuntimeConfig();
const isSub = isSubdomainRequest();
return transformRoutes(_routes, isSub, ["/about"]);
},
};