Skip to content

Commit

Permalink
fix: svg not found causes page crash (#598)
Browse files Browse the repository at this point in the history
  • Loading branch information
orionna319 authored Jan 16, 2024
1 parent 6a56fe3 commit fd55e89
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 29 deletions.
23 changes: 4 additions & 19 deletions src/components/SVGStat/index.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,12 @@
import { ComponentType, lazy, Suspense } from 'react'
import { lazy, Suspense } from 'react'
import styles from './style.module.scss';
import { totalStat } from '@assets/index'
import { loadSvgComponent } from '@/utils/svgUtils';

// Lazy load both github.svg and grid.svg
const GithubSvg = lazy(() =>
totalStat['./github.svg']()
.then((res) => {
return { default: res as ComponentType<any> }
})
.catch(() => {
return { default: () => <div>Failed to load SVG</div> };
})
)
const GithubSvg = lazy(() => loadSvgComponent(totalStat, './github.svg'))

const GridSvg = lazy(() =>
totalStat['./grid.svg']()
.then((res) => {
return { default: res as ComponentType<any> }
})
.catch(() => {
return { default: () => <div>Failed to load SVG</div> };
})
)
const GridSvg = lazy(() => loadSvgComponent(totalStat, './grid.svg'))

const SVGStat = () => (
<div id="svgStat">
Expand Down
14 changes: 4 additions & 10 deletions src/components/YearStat/index.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
import { ComponentType, lazy, Suspense } from 'react';
import { lazy, Suspense } from 'react';
import Stat from '@/components/Stat';
import useActivities from '@/hooks/useActivities';
import { formatPace } from '@/utils/utils';
import styles from './style.module.scss';
import useHover from '@/hooks/useHover';
import { yearStats } from '@assets/index'
import { yearStats } from '@assets/index';
import { loadSvgComponent } from '@/utils/svgUtils';

const YearStat = ({ year, onClick }: { year: string, onClick: (_year: string) => void }) => {
let { activities: runs, years } = useActivities();
// for hover
const [hovered, eventHandlers] = useHover();
// lazy Component
const YearSVG = lazy(() =>
yearStats[`./year_${year}.svg`]()
.then((res) => ({ default: res as ComponentType<any> }))
.catch((err) => {
console.error(err);
return { default: () => <div>Failed to load SVG</div> };
})
);
const YearSVG = lazy(() => loadSvgComponent(yearStats, `./year_${year}.svg`));

if (years.includes(year)) {
runs = runs.filter((run) => run.start_date_local.slice(0, 4) === year);
Expand Down
20 changes: 20 additions & 0 deletions src/utils/svgUtils.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { ComponentType } from 'react';

type SvgComponent = {
default: ComponentType<any>;
};

const FailedLoadSvg = () => <div>Failed to load SVG</div>;

export const loadSvgComponent = async (
stats: Record<string, () => Promise<unknown>>,
path: string
): Promise<SvgComponent> => {
try {
const module = await stats[path]();
return { default: module as ComponentType<any> };
} catch (error) {
console.error(error);
return { default: FailedLoadSvg };
}
};

1 comment on commit fd55e89

@vercel
Copy link

@vercel vercel bot commented on fd55e89 Jan 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

running-page – ./

running-page-git-master-yihong0618.vercel.app
running-page.vercel.app
running-page-yihong0618.vercel.app

Please sign in to comment.