diff --git a/package.json b/package.json index c0e5af7e..68b7e738 100644 --- a/package.json +++ b/package.json @@ -58,6 +58,7 @@ "react-dom": "^17.0.2", "react-gist": "^1.2.4", "react-innertext": "^1.1.5", + "react-intersection-observer": "^8.33.1", "react-is": "^17.0.2", "react-player": "^2.9.0", "react-tweet-embed": "^1.3.1", diff --git a/pages/notes/[slug].tsx b/pages/notes/[slug].tsx index 7e53985e..f8f020dc 100644 --- a/pages/notes/[slug].tsx +++ b/pages/notes/[slug].tsx @@ -1,6 +1,6 @@ import { useMemo } from "react"; +import { useInView } from "react-intersection-observer"; import dynamic from "next/dynamic"; -import Head from "next/head"; import { NextSeo, ArticleJsonLd } from "next-seo"; import { escape } from "html-escaper"; import { getMDXComponent } from "mdx-bundler/client"; @@ -16,18 +16,14 @@ const Comments = dynamic(() => import("../../components/notes/Comments"), { ssr: const Note = ({ frontMatter, mdxSource }: NoteType) => { const MDXComponent = useMemo(() => getMDXComponent(mdxSource, { process }), [mdxSource]); + const [commentsRef, commentsInView] = useInView({ + rootMargin: "100px", // start loading utteranc.es script 100px from end of content + triggerOnce: true, // don't unload one it's loaded (if user scrolls back up) + fallbackInView: true, + }); return ( <> - {/* preload here instead of Comments.tsx -- by the time it's loaded dynamically, there's no real point anymore */} - {frontMatter.noComments !== true && ( -
- - - - - )} -