Skip to content

Commit f7cae02

Browse files
fix: look for image on url head before rendering preview (#6127)
Co-authored-by: Diego Mello <[email protected]>
1 parent e4bb1a8 commit f7cae02

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

app/containers/message/Urls.tsx

+26-7
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,33 @@ const Url = ({ url }: { url: IUrl }) => {
128128
const { colors, theme } = useTheme();
129129
const { baseUrl, user } = useContext(MessageContext);
130130
const API_Embed = useAppSelector(state => state.settings.API_Embed);
131+
const [imageUrl, setImageUrl] = useState('');
132+
133+
useEffect(() => {
134+
const verifyUrlIsImage = async () => {
135+
try {
136+
const imageUrl = getImageUrl();
137+
if (!imageUrl) return;
138+
139+
const response = await fetch(imageUrl, { method: 'HEAD' });
140+
const contentType = response.headers.get('content-type');
141+
if (contentType?.startsWith?.('image/')) {
142+
setImageUrl(imageUrl);
143+
}
144+
} catch {
145+
// do nothing
146+
}
147+
};
148+
verifyUrlIsImage();
149+
}, [url.image, url.url]);
150+
131151
const getImageUrl = () => {
132-
const imageUrl = url.image || url.url;
152+
const _imageUrl = url.image || url.url;
133153

134-
if (!imageUrl) return null;
135-
if (imageUrl.includes('http')) return imageUrl;
136-
return `${baseUrl}/${imageUrl}?rc_uid=${user.id}&rc_token=${user.token}`;
154+
if (!_imageUrl) return null;
155+
if (_imageUrl.includes('http')) return _imageUrl;
156+
return `${baseUrl}/${_imageUrl}?rc_uid=${user.id}&rc_token=${user.token}`;
137157
};
138-
const image = getImageUrl();
139158

140159
const onPress = () => openLink(url.url, theme);
141160

@@ -166,9 +185,9 @@ const Url = ({ url }: { url: IUrl }) => {
166185
]}
167186
background={Touchable.Ripple(colors.surfaceNeutral)}>
168187
<>
169-
{image ? (
188+
{imageUrl ? (
170189
<WidthAwareView>
171-
<UrlImage image={image} hasContent={hasContent} />
190+
<UrlImage image={imageUrl} hasContent={hasContent} />
172191
</WidthAwareView>
173192
) : null}
174193
{hasContent ? <UrlContent title={url.title} description={url.description} /> : null}

0 commit comments

Comments
 (0)