Bläddra i källkod

perf: optimize blog speed, fix mobile layout shift and emoji input

- Enable ISR caching (1h) for blog list and post pages
- Deduplicate Notion API calls with React.cache
- Move notion/prism CSS from global layout to blog layout
- Optimize blog cover images with correct sizes and dimensions
- Fix mobile layout shift by hiding grid until real width is ready
- Fix emoji input on mobile by adding onTouchStart stopPropagation
- Reduce loading spinner size from 80px to 50px

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
rriecc 9 månader sedan
förälder
incheckning
bc320abef0

+ 1 - 1
app/blog/[blogId]/page.tsx

@@ -4,7 +4,7 @@ import { NotionPage } from "@/components/notion-page";
 import { getPageContent } from "@/lib/notion";
 import { siteConfig } from "@/config/site";
 
-export const revalidate = 0;
+export const revalidate = 3600;
 
 export async function generateMetadata({
   params,

+ 3 - 0
app/blog/layout.tsx

@@ -1,3 +1,6 @@
+import "react-notion-x/src/styles.css";
+import "prismjs/themes/prism-tomorrow.css";
+
 export default function BlogLayout({
   children,
 }: {

+ 1 - 1
app/blog/loading.tsx

@@ -3,7 +3,7 @@ import { HashLoader } from "react-spinners";
 export default function BlogLoading() {
   return (
     <div className="flex justify-center items-center min-h-screen">
-      <HashLoader color="#eef0f7" size={80} />
+      <HashLoader color="#eef0f7" size={50} />
     </div>
   );
 }

+ 1 - 1
app/blog/page.tsx

@@ -4,7 +4,7 @@ import { customMapImageUrl, getAllBlogPosts } from "@/lib/notion";
 import { notionBlogConfig } from "@/config/site";
 import { BlogList } from "@/components/blog-list";
 
-export const revalidate = 0;
+export const revalidate = 3600;
 
 export const metadata: Metadata = {
   title: "Blog",

+ 0 - 2
app/layout.tsx

@@ -1,7 +1,5 @@
 import "react-grid-layout/css/styles.css";
 import "react-resizable/css/styles.css";
-import "react-notion-x/src/styles.css";
-import "prismjs/themes/prism-tomorrow.css";
 import "@/styles/globals.css";
 
 import { Metadata, Viewport } from "next";

+ 7 - 1
components/animated-emoji.tsx

@@ -19,7 +19,7 @@ const AnimatedEmoji = () => {
     <div className="w-full h-full p-4 flex flex-col justify-between items-center">
       {loading ? (
         <div className="flex h-full w-full justify-center items-center">
-          <HashLoader color="#eef0f7" size={80} />
+          <HashLoader color="#eef0f7" size={50} />
         </div>
       ) : (
         <Image alt="Animated Emoji" height={150} src={emoji.url} width={150} />
@@ -37,6 +37,9 @@ const AnimatedEmoji = () => {
           onMouseDown={(e) => {
             e.stopPropagation();
           }}
+          onTouchStart={(e) => {
+            e.stopPropagation();
+          }}
           onValueChange={(value) => {
             setPrompt(value);
           }}
@@ -48,6 +51,9 @@ const AnimatedEmoji = () => {
           onMouseDown={(e) => {
             e.stopPropagation();
           }}
+          onTouchStart={(e) => {
+            e.stopPropagation();
+          }}
           onPress={async () => {
             setLoading(true);
             const response = await fetch("/api/emoji", {

+ 4 - 3
components/blog-list.tsx

@@ -33,11 +33,12 @@ export function BlogList({ blogPosts }: BlogListProps) {
             <Card className="dark:bg-darkBg dark:border-2 dark:border-knight rounded-[2rem]">
               <CardBody className="p-0">
                 <Image
-                  alt="cover"
+                  alt={title}
                   className="rounded-b-none object-cover h-[200px]"
-                  height={500}
+                  height={200}
+                  sizes="(max-width: 768px) 100vw, (max-width: 1024px) 50vw, 33vw"
                   src={imageUrl}
-                  width={500}
+                  width={400}
                 />
               </CardBody>
               <CardFooter className="flex justify-between">

+ 5 - 2
components/home-client.tsx

@@ -84,7 +84,7 @@ export default function HomeClient({
   chatbotUrl,
   paperUrl,
 }: HomeClientProps) {
-  const width = useWindowWidth();
+  const { width, ready } = useWindowWidth();
   const [tabSelected, setTabSelected] = useState("all");
   const [, startTransition] = useTransition();
   const router = useRouter();
@@ -127,7 +127,10 @@ export default function HomeClient({
 
       <Responsive
         breakpoints={{ lg: 1200, md: 996, sm: 768, xs: 480, xxs: 0 }}
-        className="layout w-full h-full"
+        className={cn(
+          "layout w-full h-full transition-opacity duration-300",
+          ready ? "opacity-100" : "opacity-0",
+        )}
         cols={{ lg: 4, md: 4, sm: 2, xs: 2, xxs: 2 }}
         layouts={layouts[tabSelected]}
         margin={[15, 15]}

+ 1 - 1
components/home.tsx

@@ -46,7 +46,7 @@ const Home = ({
   chatbotUrl,
   paperUrl,
 }: HomeProps) => {
-  const width = useWindowWidth();
+  const { width } = useWindowWidth();
   const [tabSelected, setTabSelected] = useState("all");
   const [animated, setAnimated] = useState(false);
   const router = useRouter();

+ 1 - 1
components/loading.tsx

@@ -3,7 +3,7 @@ import { HashLoader } from "react-spinners";
 const Loading = () => {
   return (
     <div className="fixed inset-0 flex items-center justify-center">
-      <HashLoader color="#eef0f7" size={80} />
+      <HashLoader color="#eef0f7" size={50} />
     </div>
   );
 };

+ 4 - 2
hooks/useWindowWidth.ts

@@ -3,8 +3,9 @@ import { useState, useEffect } from "react";
 // Default to 1280 (max container width) for SSR so server renders meaningful HTML
 const SSR_DEFAULT_WIDTH = 1280;
 
-function useWindowWidth(): number {
+function useWindowWidth(): { width: number; ready: boolean } {
   const [width, setWidth] = useState<number>(SSR_DEFAULT_WIDTH);
+  const [ready, setReady] = useState(false);
 
   useEffect(() => {
     const handleResize = () => {
@@ -12,13 +13,14 @@ function useWindowWidth(): number {
     };
 
     handleResize();
+    setReady(true);
 
     window.addEventListener("resize", handleResize);
 
     return () => window.removeEventListener("resize", handleResize);
   }, []);
 
-  return width;
+  return { width, ready };
 }
 
 export default useWindowWidth;

+ 4 - 2
lib/notion.ts

@@ -1,3 +1,5 @@
+import { cache } from "react";
+
 import { NotionAPI } from "notion-client";
 import { Block } from "notion-types";
 import { getPageTitle } from "notion-utils";
@@ -7,13 +9,13 @@ import { notionBlogConfig } from "@/config/site";
 
 const notion = new NotionAPI();
 
-export async function getPageContent(pageId: string) {
+export const getPageContent = cache(async (pageId: string) => {
   const recordMap = await notion.getPage(pageId);
   const title = getPageTitle(recordMap);
   const blocks = recordMap.block;
 
   return { title, blocks, recordMap };
-}
+});
 
 export async function getAllBlogPosts(pageId: string) {
   const recordMap = await notion.getPage(pageId);