Skip to content

Commit

Permalink
INV-50 와일드카드 서브도메인 rewrite 라우팅 처리 (#25)
Browse files Browse the repository at this point in the history
* remove: redirect 로직

* feat: middleware에서 wildcard 서브도메인 rewrite로직

* chore: 환경변수 추가

* 코드정리

* chore: ROOT_DOMAIN 환경변수 제거
  • Loading branch information
WooWan authored Aug 13, 2024
1 parent a4f558c commit 58e1ed8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
6 changes: 1 addition & 5 deletions next.config.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
/** @type {import('next').NextConfig} */
const nextConfig = {
redirects: async () => {
return [];
},
};
const nextConfig = {};

export default nextConfig;
3 changes: 3 additions & 0 deletions src/app/(playground)/pg/[subdomain]/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Page({ params }: { params: { subdomain: string } }) {
return <div>Subdomain: {params.subdomain}</div>;
}
29 changes: 29 additions & 0 deletions src/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import type { NextRequest } from "next/server";
import { NextResponse } from "next/server";

export const config = {
matcher: ["/((?!api/|_next/|_static/|_vercel|[\\w-]+\\.\\w+).*)"],
};

export function middleware(request: NextRequest) {
const url = request.nextUrl;
const hostname = request.headers.get("host")!;
const searchParams = request.nextUrl.searchParams.toString();

const path = `${url.pathname}${
searchParams.length > 0 ? `?${searchParams}` : ""
}`;

const subDomain = hostname.split(".")[0];
console.log("subDomain", subDomain, "hostname", hostname);

switch (true) {
case subDomain !== hostname:
return NextResponse.rewrite(
new URL(`/pg/${subDomain}${path}`, request.url),
);

default:
return NextResponse.rewrite(new URL(`${path}`, request.url));
}
}

0 comments on commit 58e1ed8

Please sign in to comment.