feat: force redirect to custom domain
This commit is contained in:
parent
86f4496d75
commit
b3f599fbff
|
|
@ -31,8 +31,8 @@ export const SiteHome: React.FC<{
|
|||
</div>
|
||||
</div>
|
||||
<div className="xlog-post-cover flex items-center">
|
||||
{/* eslint-disable-next-line @next/next/no-img-element */}
|
||||
{post.cover && (
|
||||
/* eslint-disable-next-line @next/next/no-img-element */
|
||||
<img
|
||||
className="object-cover w-24 h-24 rounded ml-4"
|
||||
alt="cover"
|
||||
|
|
|
|||
|
|
@ -8,7 +8,9 @@ export const getTenant = async (request: Request, search: URLSearchParams) => {
|
|||
}
|
||||
|
||||
if (search.has("tenant")) {
|
||||
return search.get("tenant")
|
||||
return {
|
||||
subdomain: search.get("tenant"),
|
||||
}
|
||||
}
|
||||
|
||||
const OUR_DOMAIN_SUFFIX = `.${OUR_DOMAIN}`
|
||||
|
|
@ -18,7 +20,26 @@ export const getTenant = async (request: Request, search: URLSearchParams) => {
|
|||
}
|
||||
if (host.endsWith(OUR_DOMAIN_SUFFIX)) {
|
||||
const subdomain = host.replace(OUR_DOMAIN_SUFFIX, "")
|
||||
return subdomain
|
||||
const res = await fetch(
|
||||
`https://indexer.crossbell.io/v1/handles/${subdomain}/character`,
|
||||
)
|
||||
const char = await res.json()
|
||||
const customDomain =
|
||||
char?.metadata?.content?.attributes?.find(
|
||||
(a: any) => a.trait_type === "xlog_custom_domain",
|
||||
)?.value || ""
|
||||
if (customDomain) {
|
||||
return {
|
||||
redirect: /^https?:\/\//.test(customDomain)
|
||||
? customDomain
|
||||
: `https://${customDomain}`,
|
||||
subdomain: subdomain,
|
||||
}
|
||||
} else {
|
||||
return {
|
||||
subdomain: subdomain,
|
||||
}
|
||||
}
|
||||
}
|
||||
if (host !== OUR_DOMAIN) {
|
||||
const res = await fetch(
|
||||
|
|
@ -41,7 +62,9 @@ export const getTenant = async (request: Request, search: URLSearchParams) => {
|
|||
(a: any) => a.trait_type === "xlog_custom_domain",
|
||||
)?.value || ""
|
||||
if (customDomain && customDomain === host) {
|
||||
return tenant
|
||||
return {
|
||||
subdomain: tenant,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -41,14 +41,20 @@ export default async function middleware(req: NextRequest) {
|
|||
|
||||
const tenant = await getTenant(req, req.nextUrl.searchParams)
|
||||
|
||||
if (tenant?.redirect && IS_PROD) {
|
||||
return NextResponse.redirect(
|
||||
`${tenant.redirect}${req.nextUrl.pathname}${req.nextUrl.search}`,
|
||||
)
|
||||
}
|
||||
|
||||
if (pathname.startsWith("/api/") || pathname.startsWith("/dashboard")) {
|
||||
return NextResponse.next()
|
||||
}
|
||||
|
||||
if (tenant) {
|
||||
if (tenant?.subdomain) {
|
||||
const url = req.nextUrl.clone()
|
||||
if (url.pathname !== "/ipfs-gateway-sw.js") {
|
||||
url.pathname = `/_site/${tenant}${url.pathname}`
|
||||
url.pathname = `/_site/${tenant?.subdomain}${url.pathname}`
|
||||
}
|
||||
return NextResponse.rewrite(url)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue