-
Notifications
You must be signed in to change notification settings - Fork 344
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Enable custom domains for shareable links
- Loading branch information
1 parent
b8367cc
commit c6aac14
Showing
5 changed files
with
68 additions
and
5 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
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
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
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,50 @@ | ||
import { NextResponse } from 'next/server'; | ||
import type { NextRequest } from 'next/server'; | ||
import { db } from '@cap/database'; | ||
import { spaces } from '@cap/database/schema'; | ||
import { eq } from 'drizzle-orm'; | ||
|
||
export async function middleware(request: NextRequest) { | ||
const hostname = request.headers.get('host'); | ||
const path = request.nextUrl.pathname; | ||
|
||
if (!hostname) return NextResponse.next(); | ||
|
||
// Skip for main domains | ||
if (hostname.includes('cap.so') || hostname.includes('cap.link') || hostname.includes('localhost')) { | ||
return NextResponse.next(); | ||
} | ||
|
||
// We're on a custom domain at this point | ||
// Only allow /s/ routes for custom domains | ||
if (!path.startsWith('/s/')) { | ||
const url = new URL(request.url); | ||
url.hostname = 'cap.so'; | ||
return NextResponse.redirect(url); | ||
} | ||
|
||
try { | ||
// Query the space with this custom domain | ||
const [space] = await db | ||
.select() | ||
.from(spaces) | ||
.where(eq(spaces.customDomain, hostname)); | ||
|
||
if (!space || !space.domainVerified) { | ||
// If no verified custom domain found, redirect to main domain | ||
const url = new URL(request.url); | ||
url.hostname = 'cap.so'; | ||
return NextResponse.redirect(url); | ||
} | ||
|
||
// Allow the request to continue to the destination | ||
return NextResponse.next(); | ||
} catch (error) { | ||
console.error('Error in middleware:', error); | ||
return NextResponse.next(); | ||
} | ||
} | ||
|
||
export const config = { | ||
matcher: ['/((?!api|_next/static|_next/image|favicon.ico).*)'] | ||
}; |
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
c6aac14
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.
Successfully deployed to the following URLs:
cap-web – ./
cap-web-git-main-mc-ilroy.vercel.app
cap-web-mc-ilroy.vercel.app
sharetest.mcilroy.co
cap-web-nu.vercel.app
share.mcilroy.co
www.cap.so
cap.so