-
Notifications
You must be signed in to change notification settings - Fork 253
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(clerk-js): Optimize image delivery
* fix(clerk-js): Replace deprecated global JSX with React.JSX * fix(clerk-js): Introduce makeResponsive HOC Primitives shouldn't be aware of any Clerk-specific logic. All the image optimizatiion logic is now extracted into its own makeResponsive HOC. * fix(clerk-js): Remove Avatar.optimise prop and isRetina helper * Create wise-turtles-study.md * fix(clerk-js): Remove Avatar.optimse prop and isRetina helper
- Loading branch information
1 parent
8b71b46
commit 59bc649
Showing
14 changed files
with
96 additions
and
96 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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@clerk/clerk-js": patch | ||
"@clerk/shared": patch | ||
--- | ||
|
||
Optimize all images displayed within the Clerk components, such as Avatars, static OAuth provider assets etc. All images are now resized and compressed. Additionally, all images are automatically converted into more efficient formats (`avif`, `webp`) if they are supported by the user's browser, otherwise all images fall back to `jpeg`. |
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
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
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,64 @@ | ||
import React from 'react'; | ||
|
||
import { isDataUri, isValidUrl } from '../../utils'; | ||
|
||
type Responsive<T = Record<never, never>> = T & { | ||
size?: number; | ||
xDescriptors?: number[]; | ||
}; | ||
|
||
type ResponsivePrimitive<T> = React.FunctionComponent<Responsive<T>>; | ||
|
||
export const makeResponsive = <P extends React.JSX.IntrinsicElements['img']>( | ||
Component: React.FunctionComponent<P>, | ||
): ResponsivePrimitive<P> => { | ||
const responsiveComponent = React.forwardRef((props: Responsive<any>, ref) => { | ||
const { src, size = 80, xDescriptors = [1, 2], ...restProps } = props; | ||
const shouldOptimize = isClerkImage(src); | ||
|
||
return ( | ||
<Component | ||
srcSet={shouldOptimize ? generateSrcSet({ src, width: size, xDescriptors }) : undefined} | ||
src={shouldOptimize ? generateSrc({ src, width: size * 2 }) : src} | ||
{...restProps} | ||
ref={ref} | ||
/> | ||
); | ||
}); | ||
|
||
const displayName = Component.displayName || Component.name || 'Component'; | ||
responsiveComponent.displayName = `Responsive${displayName}`.replace('_', ''); | ||
return responsiveComponent as ResponsivePrimitive<P>; | ||
}; | ||
|
||
const CLERK_IMAGE_URL_BASES = [ | ||
'https://img.clerk.com/', | ||
'https://img.clerk.dev/', | ||
'https://img.clerkstage.dev/', | ||
'https://img.lclclerk.com/', | ||
]; | ||
|
||
const isClerkImage = (src?: string): boolean => { | ||
return !!CLERK_IMAGE_URL_BASES.some(base => src?.includes(base)); | ||
}; | ||
|
||
const generateSrcSet = ({ src, width, xDescriptors }: { src?: string; width: number; xDescriptors: number[] }) => { | ||
if (!src) { | ||
return ''; | ||
} | ||
|
||
return xDescriptors.map(i => `${generateSrc({ src, width: width * i })} ${i}x`).toString(); | ||
}; | ||
|
||
const generateSrc = ({ src, width }: { src?: string; width: number }) => { | ||
if (!isValidUrl(src) || isDataUri(src)) { | ||
return src; | ||
} | ||
|
||
const newSrc = new URL(src); | ||
if (width) { | ||
newSrc.searchParams.append('width', width?.toString()); | ||
} | ||
|
||
return newSrc.href; | ||
}; |
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
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
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
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 was deleted.
Oops, something went wrong.