-
Notifications
You must be signed in to change notification settings - Fork 619
[Ecosystem Portal] Refactor: Update the middleware to redirect to subroutes rather than rewrites #5405
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
Closed
gregfromstl
wants to merge
2
commits into
main
from
greg/cnct-2344-update-the-ecosystem-portal-to-use-a-simple-redirect-and
Closed
[Ecosystem Portal] Refactor: Update the middleware to redirect to subroutes rather than rewrites #5405
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
| @@ -1,10 +1,13 @@ | ||
| import { getCurrentUser } from "@/lib/auth"; | ||
| import { redirect } from "next/navigation"; | ||
|
|
||
| export default async function Page() { | ||
| const user = await getCurrentUser(); | ||
| if (user) { | ||
| redirect("/wallet/${user}"); | ||
| export default async function Page({ | ||
| params, | ||
| }: { params: Promise<{ ecosystem: string }> }) { | ||
| const { ecosystem } = await params; | ||
| const address = await getCurrentUser(); | ||
| if (address) { | ||
| redirect(`${ecosystem}/wallet/${address}`); | ||
| } | ||
| redirect("/login"); | ||
| redirect(`${ecosystem}/login`); | ||
| } |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
| @@ -1,36 +1,25 @@ | ||
| import { type NextRequest, NextResponse } from "next/server"; | ||
|
|
||
| export const config = { | ||
| matcher: [ | ||
| /* | ||
| * Match all paths except for: | ||
| * 1. /api routes | ||
| * 2. /_next (Next.js internals) | ||
| * 3. /_static (inside /public) | ||
| * 4. all root files inside /public (e.g. /favicon.ico) | ||
| */ | ||
| "/((?!api/|_next/|_static/|_vercel|[\\w-]+\\.\\w+).*)", | ||
| ], | ||
| }; | ||
| export const config = { matcher: "/((?!.*\\.).*)" }; | ||
|
|
||
| const ROOT_DOMAIN = process.env.NEXT_PUBLIC_ROOT_DOMAIN; | ||
| export default async function middleware(req: NextRequest) { | ||
| // Get the request hostname (e.g. demo.thirdweb.com) | ||
| const hostname = req.headers.get("host"); | ||
| export function middleware(request: NextRequest) { | ||
| const url = request.nextUrl; | ||
| const hostname = request.headers.get("host") || ""; | ||
|
|
||
| const searchParams = req.nextUrl.searchParams.toString(); | ||
| const url = req.nextUrl; | ||
| const path = `${url.pathname}${ | ||
| searchParams.length > 0 ? `?${searchParams}` : "" | ||
| }`; | ||
| // Match pattern: something.ecosystem.domain.tld | ||
| const match = hostname.match(/^([^.]+)\.ecosystem\.([^.]+\.[^.]+)$/); | ||
|
|
||
| // keep root application at `/` | ||
| if (hostname === ROOT_DOMAIN || hostname === null) { | ||
| return NextResponse.next(); | ||
| } | ||
| if (match) { | ||
| const [_, subdomain, primaryDomain] = match; | ||
|
|
||
| // Redirect to ecosystem.domain.tld/subdomain | ||
| const newUrl = new URL(`${url.protocol}//ecosystem.${primaryDomain}`); | ||
| newUrl.pathname = `/${subdomain}${url.pathname}`; | ||
| newUrl.search = url.search; | ||
|
|
||
| // rewrite everything else to `/[ecosystem]/... dynamic route | ||
| const ecosystem = hostname.split(".")[0]; | ||
| // 308 is permanent redirect, preserves request method | ||
| return NextResponse.redirect(newUrl, 308); | ||
| } | ||
|
|
||
| return NextResponse.rewrite(new URL(`/${ecosystem}${path}`, req.url)); | ||
| return NextResponse.next(); | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The redirect path is missing a leading forward slash, which could cause navigation issues. The correct path should be:
Spotted by Graphite Reviewer
Is this helpful? React 👍 or 👎 to let us know.