Skip to content

Commit

Permalink
Use next/dynamic and remove hack for removing dynamic data
Browse files Browse the repository at this point in the history
  • Loading branch information
marshallku committed May 6, 2024
1 parent d8ab865 commit e5d9917
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 18 deletions.
5 changes: 4 additions & 1 deletion apps/blog/app/[category]/[...slug]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Metadata } from "next";
import { notFound } from "next/navigation";
import nextDynamic from "next/dynamic";
import Link from "next/link";
import { MDXRemote } from "next-mdx-remote/rsc";
import rehypeAutolinkHeadings from "rehype-autolink-headings";
Expand All @@ -14,7 +15,6 @@ import { classNames, formatDate } from "@marshallku/utils";
import MDXComponents from "#components/MDXComponents";
import InteractPost from "#components/InteractPost";
import Image from "#components/Image";
import PostComment from "#components/PostComment";
import PostList from "#components/PostList";
import PrevNextPost from "#components/PrevNextPost";
import Typography from "#components/Typography";
Expand Down Expand Up @@ -72,6 +72,9 @@ export async function generateStaticParams() {
const cx = classNames(styles, "page");

export default async function Post({ params: { category, slug } }: PostProps) {
const PostComment = nextDynamic(() => import("#components/PostComment"), {
ssr: false,
});
const postSlug = `${category}/${slug.map((x) => decodeURIComponent(x)).join("/")}`;
const post = getPostBySlug(postSlug);

Expand Down
24 changes: 7 additions & 17 deletions apps/blog/src/components/PostComment/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use client";

import { Suspense, useEffect, useMemo, useState } from "react";
import { Suspense, useMemo } from "react";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { ErrorBoundary } from "@marshallku/react-error-boundary";
import { classNames } from "@marshallku/utils";
Expand All @@ -16,26 +16,16 @@ const cx = classNames(styles, "post-comment");

function PostComment({ slug }: PostCommentProps) {
const queryClient = useMemo(() => new QueryClient(), []);
const [render, setRender] = useState(false);

useEffect(() => {
setRender(true);
}, []);

return (
<div className={cx()}>
<QueryClientProvider client={queryClient}>
{/* HACK: Removing dynamic data while building application */}
{render && (
<>
<PostCommentForm slug={slug} />
<ErrorBoundary fallback={null}>
<Suspense fallback={null}>
<Comments slug={slug} />
</Suspense>
</ErrorBoundary>
</>
)}
<PostCommentForm slug={slug} />
<ErrorBoundary fallback={null}>
<Suspense fallback={null}>
<Comments slug={slug} />
</Suspense>
</ErrorBoundary>
</QueryClientProvider>
</div>
);
Expand Down

0 comments on commit e5d9917

Please sign in to comment.