Skip to content

Commit

Permalink
Better error handling in Favicon component
Browse files Browse the repository at this point in the history
  • Loading branch information
OlegWock committed Apr 28, 2023
1 parent 57fd1d6 commit 74b0606
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/components/Icon.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ComponentPropsWithoutRef, RefAttributes, useLayoutEffect, useMemo } from 'react';
import { ComponentPropsWithoutRef, RefAttributes, useLayoutEffect, useMemo, useState } from 'react';
import browser from 'webextension-polyfill';
import { allSets } from './icons/all-sets';
import { IconifyJSON, Icon as OfflineIcon, addCollection } from '@iconify/react/dist/offline';
Expand Down Expand Up @@ -122,15 +122,18 @@ export const Icon = forwardRef<RefAttributes<SVGSVGElement>, IconProps>((props,
type FaviconProps = {
url: string,
fallback?: string,
useFaviconApiIfPossible?: boolean,
} & BaseIconProps;

export const Favicon = forwardRef<HTMLElement, FaviconProps>((props, ref) => {
const permissions = useAtomValue(availablePermissionsAtom);
// @ts-expect-error new permission not yet in types
const hasPermission = permissions?.permissions.includes('favicon');
const [imageError, setImageError] = useState(false);

const iconUrl = useMemo(() => {
const size = (props.width || props.height || 64).toString();
if (hasPermission) {
if (hasPermission && props.useFaviconApiIfPossible) {
const resUrl = new URL(browser.runtime.getURL("/_favicon/"));
resUrl.searchParams.set("pageUrl", props.url);
resUrl.searchParams.set("size", size);
Expand All @@ -148,9 +151,9 @@ export const Favicon = forwardRef<HTMLElement, FaviconProps>((props, ref) => {



if (iconUrl) {
if (iconUrl && !imageError) {
// @ts-ignore incorrect ref typing
return (<img src={iconUrl} {...props} ref={ref} />);
return (<img src={iconUrl} onError={() => setImageError(true)} {...props} ref={ref} />);
}

// @ts-ignore incorrect ref typing
Expand Down
1 change: 1 addition & 0 deletions src/plugins/bookmark/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
align-items: center;
text-decoration: none;
flex-grow: 1;
max-height: 100%;

&.size-s {
flex-direction: column;
Expand Down

0 comments on commit 74b0606

Please sign in to comment.