diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index d9b66ea2..4ad4dbab 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1,4 +1,4 @@ -lockfileVersion: '6.0' +lockfileVersion: '6.1' settings: autoInstallPeers: true diff --git a/src/app/site/[site]/[slug]/page.tsx b/src/app/site/[site]/[slug]/page.tsx index ba5677f4..d793d4b5 100644 --- a/src/app/site/[site]/[slug]/page.tsx +++ b/src/app/site/[site]/[slug]/page.tsx @@ -13,8 +13,8 @@ import { getSiteLink } from "~/lib/helpers" import { getTranslation } from "~/lib/i18n" import { toCid, toGateway } from "~/lib/ipfs-parser" import { isInRN } from "~/lib/is-in-rn" +import { isOnlyContent } from "~/lib/is-only-client" import getQueryClient from "~/lib/query-client" -import { isOnlyContent } from "~/lib/search-parser" import { fetchGetPage, getSummary } from "~/queries/page.server" import { fetchGetSite } from "~/queries/site.server" @@ -103,7 +103,7 @@ export default async function SitePagePage({ } const dehydratedState = dehydrate(queryClient) - const onlyContent = isOnlyContent() + const onlyContent = await isOnlyContent() function addPageJsonLd() { return { diff --git a/src/app/site/[site]/layout.tsx b/src/app/site/[site]/layout.tsx index 74b59ce3..950fce14 100644 --- a/src/app/site/[site]/layout.tsx +++ b/src/app/site/[site]/layout.tsx @@ -13,8 +13,8 @@ import { SiteHeader } from "~/components/site/SiteHeader" import { FABContainer } from "~/components/ui/FAB" import { SITE_URL } from "~/lib/env" import { getSiteLink } from "~/lib/helpers" +import { isOnlyContent, searchParser } from "~/lib/is-only-client" import getQueryClient from "~/lib/query-client" -import { isOnlyContent } from "~/lib/search-parser" import { ExpandedNote } from "~/lib/types" import { cn } from "~/lib/utils" import { fetchGetPage } from "~/queries/page.server" @@ -93,17 +93,21 @@ export default async function SiteLayout({ }) { const queryClient = getQueryClient() + const search = await searchParser() // 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() + const onlyContent = await isOnlyContent() if (pathname && /^(\/site(?!\/.*\/preview\/).*)/.test(pathname)) { - redirect( - `${getSiteLink({ - subdomain: params.site, - })}/${pathname.replace(/\/site\/(.*)\//, "")}`, - ) + const targetPath = `${getSiteLink({ + subdomain: params.site, + })}/${pathname.replace(/\/site\/(.*)\//, "")}` + + const targetUrl = new URL(targetPath) + targetUrl.search = search.toString() + + redirect(targetUrl.toString()) } let type: string diff --git a/src/components/common/PageContent.tsx b/src/components/common/PageContent.tsx index 94247b7a..9bc3a1b4 100644 --- a/src/components/common/PageContent.tsx +++ b/src/components/common/PageContent.tsx @@ -2,13 +2,14 @@ import { type MutableRefObject, memo } from "react" import PostActions from "~/components/site/PostActions" import PostToc from "~/components/site/PostToc" +import { isOnlyContent } from "~/lib/is-only-client" import { ExpandedCharacter, ExpandedNote } from "~/lib/types" import { cn } from "~/lib/utils" import { renderPageContent } from "~/markdown" import { PageContentContainer } from "./PageContentContainer" -const PageContent = memo(function PageContent({ +const PageContent = memo(async function PageContent({ className, content, toc, @@ -40,6 +41,8 @@ const PageContent = memo(function PageContent({ inParsedContent = renderPageContent(content, false, isComment) } + const onlyContent = await isOnlyContent() + return ( {inParsedContent?.element} - {toc && inParsedContent?.toc && } - {withActions && } + {!onlyContent && toc && inParsedContent?.toc && ( + + )} + {!onlyContent && withActions && } ) diff --git a/src/lib/is-only-client.ts b/src/lib/is-only-client.ts new file mode 100644 index 00000000..0e281bd4 --- /dev/null +++ b/src/lib/is-only-client.ts @@ -0,0 +1,17 @@ +import { isServerSide } from "./utils" + +export const searchParser = async () => { + let search + if (isServerSide()) { + const { headers } = await import("next/headers") + search = headers().get("x-xlog-search") + } else { + search = window.location.search + } + + return new URLSearchParams(search?.substring(1) || "") +} + +export const isOnlyContent = async () => { + return (await searchParser()).get("only-content") +} diff --git a/src/lib/search-parser.ts b/src/lib/search-parser.ts deleted file mode 100644 index 81055f75..00000000 --- a/src/lib/search-parser.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { headers } from "next/headers" - -export const searchParser = () => { - const search = headers().get("x-xlog-search") - - return new URLSearchParams(search?.substring(1) || "") -} - -export const isOnlyContent = () => { - return searchParser().get("only-content") -}