-
Notifications
You must be signed in to change notification settings - Fork 0
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
INV-50 와일드카드 서브도메인 rewrite 라우팅 처리 #25
Changes from 2 commits
cf88f72
ad4c975
af01975
e3b17bd
dd25c3b
382f804
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,4 @@ | ||
/** @type {import('next').NextConfig} */ | ||
const nextConfig = { | ||
redirects: async () => { | ||
return [ | ||
{ | ||
source: "/", | ||
destination: "/ut", | ||
permanent: true, | ||
}, | ||
]; | ||
}, | ||
}; | ||
const nextConfig = {}; | ||
|
||
export default nextConfig; |
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>; | ||
} |
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: | ||
Comment on lines
+20
to
+21
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 혹시 여기서 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 제 취향일 수 있는데 여기에서는 switch 를 사용하는게 더 직관적인 것 같아요 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 요런 느낌인가요? swtich (true) {
case subDomain === "pg":
return NextResponse.rewrite(new URL(`/pg`, request.url));
...
}
if (subDomain === "pg") {
return return NextResponse.rewrite(new URL(`/pg`, request.url));
}
if (subDomain !== hostname) {
return return NextResponse.rewrite(new URL(`/pg/${subDomain}${path}`, request.url));
}
return NextResponse.rewrite(new URL(`${path}`, request.url)); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 저도 switch (true) 패턴을 처음봤을 때 어색해보였는데, typescript 최신 버전에서도 타입 narrowing 지원을 강화할 만큼 많이 사용하는 패턴이에요! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 오호 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 현재 에러가 있는데 한번 봐주세요. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. invi-csdgt32gp-invis-projects와 invi-csdgt32gp-invis-projects.vercel.app가 일치하지 않을 때, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as-ishttps://invi-csdgt32gp-invis-projects.vercel.app/ 가 to-behttps://invi-csdgt32gp-invis-projects.vercel.app/ 는 스크린샷There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Vercel에서 preview를 제공할 때, wildcard 서브도메인으로 제공해서 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 아아 맞네요 vercel preview는 서브도메인이기 때문에 rewrite 되는게 맞네요. 다만 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 구두로 협의한 내용: vercel은 preview를 이미 서브도메인 형식으로 제공하고 있고, 아래와 같이 |
||
console.log(`/pg/${subDomain}${path}`); | ||
return NextResponse.rewrite( | ||
new URL(`/pg/${subDomain}${path}`, request.url), | ||
); | ||
|
||
default: | ||
return NextResponse.rewrite(new URL(`${path}`, request.url)); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NEXT_PUBLIC_ROOT_DOMAIN
는 테스트용으로 따로 설정하는건가요?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
요거 필요없는 로직같아서 제거하였습니다~ dd25c3b