Skip to content

Commit

Permalink
feat: proxy frame image requests
Browse files Browse the repository at this point in the history
  • Loading branch information
JFrankfurt committed Oct 8, 2024
1 parent 5316dff commit b769976
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 52 deletions.
46 changes: 0 additions & 46 deletions apps/web/app/frames/route.tsx

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import type { FrameUIComponents, FrameUITheme } from '@frames.js/render/ui';
import classNames from 'classnames';
import Image from 'next/image';
import { useEffect, useState } from 'react';
import { useEffect, useMemo, useState } from 'react';
import baseLoading from './base-loading.gif';

type StylingProps = {
Expand Down Expand Up @@ -40,6 +40,14 @@ export const theme: FrameUITheme<StylingProps> = {
},
};

function isDataUrl(url: string) {
return /^data:image\/[a-zA-Z]+;base64,/.test(url);
}

function isSvgDataUrl(url: string) {
return url.startsWith('data:image/svg+xml');
}

type TransitionWrapperProps = {
aspectRatio: '1:1' | '1.91:1';
src: string;
Expand Down Expand Up @@ -75,6 +83,24 @@ function TransitionWrapper({

const ar = aspectRatio.replace(':', '/');

const style = useMemo(
() => ({
'--frame-image-aspect-ratio': ar,
...(isCssProperties(stylingProps.style) && stylingProps.style),
}),
[ar, stylingProps.style],
);

const assetSrc = useMemo(
() =>
isLoading || isSvgDataUrl(src)
? ''
: isDataUrl(src)
? src
: `/frames/img-proxy?url=${encodeURIComponent(src)}`,
[isLoading, src],
);

return (
<div className="relative">
{/* Loading Screen */}
Expand All @@ -90,15 +116,12 @@ function TransitionWrapper({
{/* Image */}
<img
{...stylingProps}
src={isLoading ? undefined : src}
src={assetSrc}
alt={alt}
onLoad={onImageLoadEnd}
onError={onImageLoadEnd}
data-aspect-ratio={ar}
style={{
'--frame-image-aspect-ratio': ar,
...(isCssProperties(stylingProps.style) && stylingProps.style),
}}
style={style}
className={classNames('transition-opacity duration-500', {
'opacity-0': isLoading || isTransitioning,
'opacity-100': !isLoading && !isTransitioning,
Expand Down

0 comments on commit b769976

Please sign in to comment.