Skip to content

Commit d321279

Browse files
committed
Add lazy loading
1 parent 76c3b42 commit d321279

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

components/Icon.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@ import { SmallChar } from "../utils/types"
44
import { elements, getStarColor, image, urlify, weapons } from "../utils/utils"
55
import FormattedLink from "./FormattedLink"
66

7-
export default function Icon({ icon, className }: { icon: { name: string, icon?: string }, className?: string }) {
7+
export default function Icon({ icon, className, loading = "lazy" }: { icon: { name: string, icon?: string }, className?: string, loading?: "eager" | "lazy" }) {
88
const src = icon.icon ?? "img/unknown.png"
99

1010
if (src.startsWith("img"))
1111
// eslint-disable-next-line @next/next/no-img-element
12-
return <img alt={icon.name} src={"/" + src} className={className} width={256} height={256} onError={(e) => (e.target as HTMLImageElement).src = "/img/unknown.png"} loading="eager" />
12+
return <img alt={icon.name} loading={loading} src={"/" + src} className={className} width={256} height={256} onError={(e) => (e.target as HTMLImageElement).src = "/img/unknown.png"} />
1313

14-
return <Image alt={icon.name} src={src} className={className} width={256} height={256} onError={(e) => (e.target as HTMLImageElement).src = "/img/unknown.png"} loading="eager" />
14+
return <Image alt={icon.name} loading={loading} src={src} className={className} width={256} height={256} onError={(e) => (e.target as HTMLImageElement).src = "/img/unknown.png"} />
1515
}
1616

17-
export function IconName({ name, type, urltype }: { name: string, type: string, urltype: string }) {
17+
export function IconName({ name, type, urltype, loading = "lazy" }: { name: string, type: string, urltype: string, loading: "eager" | "lazy" }) {
1818
return <FormattedLink href={`/${urltype}/${urlify(name, false)}`} className="flex flex-row align-middle items-center">
1919
<div className="pr-1 w-12 h-12 md:h-16 md:w-16">
20-
<img src={image(type, name)} alt={name} width={256} height={256} />
20+
<img src={image(type, name)} loading={loading} alt={name} width={256} height={256} />
2121
</div>
2222
<div className="font-semibold md:text-xl">{name}</div>
2323
</FormattedLink>

pages/materials/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,12 @@ export default function Materials(props: Props & { location: string }) {
138138
.filter(m => starFilter == 0 || starFilter == (m.stars ?? 1))
139139
.filter(m => categoryFilter.some(e => m.category?.includes(e)) || m.category == undefined)
140140
.filter(m => typeFilter.some(e => m.type?.includes(e)) || m.type == undefined)
141-
.map(mat => {
141+
.map((mat, i) => {
142142
const color = getStarColor(mat.stars ?? 1)
143143

144-
return <FormattedLink key={mat.name} prefetch={false} location={props.location} href={`/materials/${urlify(mat.name, false)}`} className="bg-slate-300 dark:bg-slate-800 w-24 sm:w-28 lg:w-32 m-1 relative rounded-xl transition-all duration-100 hover:outline outline-slate-800 dark:outline-slate-300 text-bold text-sm" >
144+
return <FormattedLink key={mat.name} prefetch={i < 21} location={props.location} href={`/materials/${urlify(mat.name, false)}`} className="bg-slate-300 dark:bg-slate-800 w-24 sm:w-28 lg:w-32 m-1 relative rounded-xl transition-all duration-100 hover:outline outline-slate-800 dark:outline-slate-300 text-bold text-sm" >
145145
<div className={`${color} rounded-t-xl h-24 sm:h-28 lg:h-32`}>
146-
<Icon icon={mat} className="rounded-t-xl m-0 p-0" />
146+
<Icon icon={mat} loading={i < 21 ? "eager" : "lazy"} className="rounded-t-xl m-0 p-0" />
147147
</div>
148148
<div
149149
className={`flex justify-center items-center m-0 px-1 duration-200 md:text-base ${mat.name.length > 27 ? "text-xs md:text-xs" : mat.name.length > 20 ? "text-sm md:text-sm" : ""}`}

0 commit comments

Comments
 (0)