Skip to content

Commit 8c54874

Browse files
authored
Merge pull request #213 from Moaguide-develop/feat/articleDetail
feat: image optimize by useMemo, window.Image()
2 parents 75b6888 + b36af2d commit 8c54874

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

src/components/learning/LearningPageClient.tsx

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use client';
22

3-
import React, { useEffect, useState } from 'react';
3+
import React, { useEffect, useMemo, useState } from 'react';
44
import { useQuery } from '@tanstack/react-query';
5-
import Image from 'next/image';
5+
import Image, { StaticImageData } from 'next/image';
66
import PopularContents from '@/components/learning/PopularContents';
77
import RecentContents from '@/components/learning/RecentContents';
88
import LatestNewsClipping from '@/components/learning/LatestNewsClipping';
@@ -82,13 +82,27 @@ const LearningPageClient = ({ initialData }: { initialData: OverviewResponse })
8282
}))
8383
: [];
8484

85-
const categoryInfo = categoryConfig[selectedCategory as keyof typeof categoryConfig] || categoryConfig.all;
86-
87-
85+
const categoryInfo = useMemo(() => {
86+
return categoryConfig[selectedCategory as keyof typeof categoryConfig] || categoryConfig.all;
87+
}, [selectedCategory]);
88+
89+
const preloadImage = (src: StaticImageData) => {
90+
const img = new window.Image();
91+
img.src = src.src;
92+
};
93+
94+
useEffect(() => {
95+
if (selectedCategory) {
96+
preloadImage(categoryConfig[selectedCategory as keyof typeof categoryConfig].backgroundImage);
97+
}
98+
}, [selectedCategory]);
99+
100+
88101
return (
89102
<div>
90103
<div className="hidden sm:flex relative w-full h-[300px] lg:h-[400px]">
91104
<Image
105+
key={categoryInfo.backgroundImage.src}
92106
src={categoryInfo.backgroundImage}
93107
alt="Background"
94108
layout="fill"

0 commit comments

Comments
 (0)