feat: redirect site links

This commit is contained in:
DIYgod 2023-08-04 20:01:41 +01:00
parent 2b4aa1d220
commit 189140ae41
No known key found for this signature in database
1 changed files with 9 additions and 4 deletions

View File

@ -1,6 +1,6 @@
import { Metadata } from "next"
import { headers } from "next/headers"
import { notFound } from "next/navigation"
import { notFound, redirect } from "next/navigation"
import { Hydrate, dehydrate } from "@tanstack/react-query"
@ -12,6 +12,7 @@ import SiteFooter from "~/components/site/SiteFooter"
import { SiteHeader } from "~/components/site/SiteHeader"
import { FABContainer } from "~/components/ui/FAB"
import { SITE_URL } from "~/lib/env"
import { getSiteLink } from "~/lib/helpers"
import getQueryClient from "~/lib/query-client"
import { isOnlyContent } from "~/lib/search-parser"
import { ExpandedNote } from "~/lib/types"
@ -92,15 +93,17 @@ export default async function SiteLayout({
}) {
const queryClient = getQueryClient()
const site = await fetchGetSite(params.site, queryClient)
// https://github.com/vercel/next.js/issues/46618#issuecomment-1450416633
// Issue: The type will not be updated when the page is redirected.
let pathname = headers().get("x-xlog-pathname")
const onlyContent = isOnlyContent()
if (pathname?.startsWith("/site/")) {
pathname = pathname.replace(/^\/site\/[^/]*/, "")
redirect(
`${getSiteLink({
subdomain: params.site,
})}/${pathname.replace(/\/site\/(.*)\//, "")}`,
)
}
let type: string
@ -125,6 +128,8 @@ export default async function SiteLayout({
}
}
const site = await fetchGetSite(params.site, queryClient)
let page: ExpandedNote | undefined | null
if (site?.characterId) {
await Promise.all([