-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: middleware에서 wildcard 서브도메인 rewrite로직
- Loading branch information
Showing
2 changed files
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
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")! | ||
.replace(".localhost:3000", `.${process.env.NEXT_PUBLIC_ROOT_DOMAIN}`); | ||
const searchParams = request.nextUrl.searchParams.toString(); | ||
|
||
const path = `${url.pathname}${ | ||
searchParams.length > 0 ? `?${searchParams}` : "" | ||
}`; | ||
|
||
const subDomain = hostname.split(".")[0]; | ||
|
||
switch (true) { | ||
case subDomain !== hostname: | ||
console.log(`/pg/${subDomain}${path}`); | ||
return NextResponse.rewrite( | ||
new URL(`/pg/${subDomain}${path}`, request.url), | ||
); | ||
|
||
default: | ||
return NextResponse.rewrite(new URL(`${path}`, request.url)); | ||
} | ||
} |