generated from deco-sites/start
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
937 additions
and
238 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,98 +1 @@ | ||
import { Head } from "$fresh/runtime.ts"; | ||
import { forwardRef } from "preact/compat"; | ||
import ImageKit from "https://esm.sh/[email protected]"; | ||
import type { JSX } from "preact"; | ||
|
||
type Props = | ||
& Omit<JSX.IntrinsicElements["img"], "width" | "height" | "preload"> | ||
& { | ||
width: number; | ||
height?: number; | ||
src: string; | ||
preload?: boolean; | ||
fetchPriority?: "high" | "low" | "auto"; | ||
}; | ||
|
||
const imageKit = new ImageKit({ | ||
urlEndpoint: "https://ik.imagekit.io/decocx", | ||
}); | ||
|
||
const FACTORS = [1, 1.5, 2]; | ||
|
||
export const getOptimizedMediaUrl = ( | ||
{ originalSrc, width, height, factor }: { | ||
originalSrc: string; | ||
width: number; | ||
height?: number; | ||
factor: number; | ||
}, | ||
) => { | ||
const w = `${Math.trunc(factor * width)}`; | ||
const h = height ? `${Math.trunc(factor * height)}` : undefined; | ||
|
||
if ( | ||
originalSrc.includes(".vteximg.com.br/arquivos/ids") || | ||
originalSrc.includes(".vtexassets.com/arquivos/ids") | ||
) { | ||
const url = new URL(originalSrc); | ||
|
||
const [slash, arquivos, ids, id, ...rest] = url.pathname.split("/"); | ||
const [realId] = id.split("-"); | ||
url.protocol = "https:"; | ||
url.pathname = [slash, arquivos, ids, `${realId}-${w}-${h ?? w}`, ...rest] | ||
.join("/"); | ||
|
||
return url.href; | ||
} | ||
|
||
return imageKit.url({ | ||
path: originalSrc, | ||
transformation: [{ width: w, height: h }], | ||
}); | ||
}; | ||
|
||
export const getSrcSet = (src: string, width: number, height?: number) => | ||
FACTORS | ||
.map((factor) => | ||
`${getOptimizedMediaUrl({ originalSrc: src, width, height, factor })} ${ | ||
Math.trunc(factor * width) | ||
}w` | ||
) | ||
.join(", "); | ||
|
||
const Image = forwardRef<HTMLImageElement, Props>((props, ref) => { | ||
const { preload, loading = "lazy" } = props; | ||
|
||
const srcSet = getSrcSet(props.src, props.width, props.height); | ||
const linkProps = { | ||
imagesrcset: srcSet, | ||
imagesizes: props.sizes, | ||
fetchpriority: props.fetchPriority, | ||
media: props.media, | ||
}; | ||
|
||
return ( | ||
<> | ||
{preload && ( | ||
<Head> | ||
<link | ||
as="image" | ||
rel="preload" | ||
href={props.src} | ||
{...linkProps} | ||
/> | ||
</Head> | ||
)} | ||
<img | ||
{...props} | ||
preload={undefined} | ||
src={props.src} | ||
srcSet={srcSet} | ||
loading={loading} | ||
ref={ref} | ||
/> | ||
</> | ||
); | ||
}); | ||
|
||
export default Image; | ||
export { default } from "deco-sites/std/packs/image/components/Image.tsx"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,4 @@ | ||
import { useContext, useMemo } from "preact/hooks"; | ||
import { forwardRef } from "preact/compat"; | ||
import { ComponentChildren, createContext, JSX } from "preact"; | ||
import { Head } from "$fresh/runtime.ts"; | ||
|
||
import { getSrcSet } from "./Image.tsx"; | ||
|
||
interface Context { | ||
preload?: boolean; | ||
} | ||
|
||
const Context = createContext<Context>({ | ||
preload: false, | ||
}); | ||
|
||
type SourceProps = | ||
& Omit<JSX.IntrinsicElements["source"], "width" | "height" | "preload"> | ||
& { | ||
src: string; | ||
width: number; | ||
height?: number; | ||
preload?: boolean; | ||
fetchPriority?: "high" | "low" | "auto"; | ||
}; | ||
|
||
export const Source = forwardRef<HTMLSourceElement, SourceProps>( | ||
(props, ref) => { | ||
const { preload } = useContext(Context); | ||
|
||
const srcSet = getSrcSet(props.src, props.width, props.height); | ||
const linkProps = { | ||
imagesrcset: srcSet, | ||
imagesizes: props.sizes, | ||
fetchpriority: props.fetchPriority, | ||
media: props.media, | ||
}; | ||
|
||
return ( | ||
<> | ||
{preload && ( | ||
<Head> | ||
<link | ||
as="image" | ||
rel="preload" | ||
href={props.src} | ||
{...linkProps} | ||
/> | ||
</Head> | ||
)} | ||
<source | ||
{...props} | ||
preload={undefined} | ||
src={undefined} // Avoid deprecated api lighthouse warning | ||
srcSet={srcSet} | ||
ref={ref} | ||
/> | ||
</> | ||
); | ||
}, | ||
); | ||
|
||
type Props = Omit<JSX.IntrinsicElements["picture"], "preload"> & { | ||
children: ComponentChildren; | ||
preload?: boolean; | ||
}; | ||
|
||
export const Picture = forwardRef<HTMLPictureElement, Props>( | ||
({ children, preload, ...props }, ref) => { | ||
const value = useMemo(() => ({ preload }), [preload]); | ||
|
||
return ( | ||
<Context.Provider value={value}> | ||
<picture {...props} ref={ref}> | ||
{children} | ||
</picture> | ||
</Context.Provider> | ||
); | ||
}, | ||
); | ||
export { | ||
Picture, | ||
Source, | ||
} from "deco-sites/std/packs/image/components/Picture.tsx"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1 @@ | ||
/** | ||
* TODO: Implement video preload with link[rel="preload"] tags once | ||
* browsers support it. More info at: https://stackoverflow.com/a/68368601 | ||
*/ | ||
import { forwardRef } from "preact/compat"; | ||
import type { JSX } from "preact"; | ||
|
||
import { getOptimizedMediaUrl, getSrcSet } from "./Image.tsx"; | ||
|
||
type Props = | ||
& Omit<JSX.IntrinsicElements["video"], "width" | "height" | "preload"> | ||
& { | ||
width: number; | ||
height: number; | ||
src: string; | ||
forceOptimizedSrc?: boolean; | ||
}; | ||
|
||
const Video = forwardRef<HTMLVideoElement, Props>((props, ref) => { | ||
const { loading = "lazy" } = props; | ||
const srcSet = getSrcSet(props.src, props.width, props.height); | ||
|
||
const src = props.forceOptimizedSrc | ||
? getOptimizedMediaUrl({ | ||
originalSrc: props.src, | ||
width: props.width, | ||
height: props.height, | ||
factor: 1, | ||
}) | ||
: props.src; | ||
|
||
return ( | ||
<video | ||
{...props} | ||
preload={undefined} | ||
src={src} | ||
srcSet={srcSet} | ||
loading={loading} | ||
ref={ref} | ||
/> | ||
); | ||
}); | ||
|
||
export default Video; | ||
export { default } from "deco-sites/std/packs/image/components/Video.tsx"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from "deco-sites/std/packs/image/loaders/image.ts"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { Head } from "$fresh/runtime.ts"; | ||
import type { JSX } from "preact"; | ||
import { forwardRef } from "preact/compat"; | ||
import { KEY } from "../constants.ts"; | ||
|
||
type Props = | ||
& Omit<JSX.IntrinsicElements["img"], "width" | "height" | "preload"> | ||
& { | ||
src: string; | ||
/** @description Improves Web Vitals (CLS|LCP) */ | ||
width: number; | ||
/** @description Improves Web Vitals (CLS|LCP) */ | ||
height?: number; | ||
/** @description Web Vitals (LCP). Adds a link[rel="preload"] tag in head. Use one preload per page for better performance */ | ||
preload?: boolean; | ||
/** @description Improves Web Vitals (LCP). Use high for LCP image. Auto for other images */ | ||
fetchPriority?: "high" | "low" | "auto"; | ||
}; | ||
|
||
const FACTORS = [1, 1.5, 2]; | ||
|
||
export const getOptimizedMediaUrl = ( | ||
{ originalSrc, width, height, factor }: { | ||
originalSrc: string; | ||
width: number; | ||
height?: number; | ||
factor: number; | ||
}, | ||
) => { | ||
const params = new URLSearchParams(); | ||
|
||
params.set("src", originalSrc); | ||
params.set("fit", "contain"); | ||
params.set("width", `${Math.trunc(factor * width)}`); | ||
height && params.set("height", `${Math.trunc(factor * height)}`); | ||
|
||
return `${KEY}?${params}`; | ||
}; | ||
|
||
export const getSrcSet = (src: string, width: number, height?: number) => | ||
FACTORS | ||
.map((factor) => | ||
`${getOptimizedMediaUrl({ originalSrc: src, width, height, factor })} ${ | ||
Math.trunc(factor * width) | ||
}w` | ||
) | ||
.join(", "); | ||
|
||
const Image = forwardRef<HTMLImageElement, Props>((props, ref) => { | ||
const { preload, loading = "lazy" } = props; | ||
|
||
if (!props.height) console.warn(`Missing height on image: ${props.src}`); | ||
|
||
const srcSet = getSrcSet(props.src, props.width, props.height); | ||
const linkProps = { | ||
imagesrcset: srcSet, | ||
imagesizes: props.sizes, | ||
fetchpriority: props.fetchPriority, | ||
media: props.media, | ||
}; | ||
|
||
return ( | ||
<> | ||
{preload && ( | ||
<Head> | ||
<link | ||
as="image" | ||
rel="preload" | ||
href={props.src} | ||
{...linkProps} | ||
/> | ||
</Head> | ||
)} | ||
<img | ||
{...props} | ||
preload={undefined} | ||
src={props.src} | ||
srcSet={srcSet} | ||
loading={loading} | ||
ref={ref} | ||
/> | ||
</> | ||
); | ||
}); | ||
|
||
export default Image; |
Oops, something went wrong.