File size: 1,304 Bytes
75fefa7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/* eslint-disable @next/next/no-img-element */
import { ComponentProps } from "react";

import compressorConfig from "@/public/compressor.json";

interface Props extends ComponentProps<"img"> {
  src: string;
  alt: string;
  raw?: boolean;
}

const BASE_SRC = "/assets/";
const RAW_SRC = "/assets-original/";

export default function Image({ src, raw, ...attrs }: Props) {
  if (raw) {
    return (
      <img
        {...attrs}
        alt={attrs.alt}
        decoding="async"
        loading="lazy"
        src={RAW_SRC + src + ".png"}
      />
    );
  }

  return (
    <picture>
      {compressorConfig.configs
        .sort((a, b) => {
          if (a.extension === "avif" && b.extension !== "avif") return -1;
          if (b.extension === "avif" && a.extension !== "avif") return 1;

          return a.scale - b.scale;
        })
        .map((c) => {
          return (
            <source
              key={`${c.extension}_q${c.quality}@${c.scale}x`}
              srcSet={`${BASE_SRC}${src}_q${c.quality}@${c.scale}x.${c.extension}`}
              type={`image/${c.extension}`}
            />
          );
        })}

      <img
        {...attrs}
        alt={attrs.alt}
        decoding="async"
        loading="lazy"
        src={`${BASE_SRC}${src}.png`}
      />
    </picture>
  );
}