1
mirror of https://github.com/jakejarvis/jarv.is.git synced 2025-07-21 07:01:19 -04:00

wrap hit counter with an error boundary in case the API falls on its face again

This commit is contained in:
2024-01-21 13:22:14 -05:00
parent dbebd35c4d
commit 5b0c126ffc
5 changed files with 284 additions and 300 deletions

View File

@@ -1,4 +1,5 @@
import useSWRImmutable from "swr/immutable";
import { useErrorBoundary } from "react-error-boundary";
import commaNumber from "comma-number";
import Loading from "../Loading";
import fetcher from "../../lib/helpers/fetcher";
@@ -9,6 +10,8 @@ export type HitCounterProps = {
};
const HitCounter = ({ slug }: HitCounterProps) => {
const { showBoundary } = useErrorBoundary();
// use immutable SWR to avoid double (or more) counting views:
// https://swr.vercel.app/docs/revalidation#disable-automatic-revalidations
const { data, error } = useSWRImmutable<PageStats>(
@@ -18,8 +21,9 @@ const HitCounter = ({ slug }: HitCounterProps) => {
fetcher
);
// fail secretly
// fail somewhat silently, see error boundary in NoteMeta component
if (error) {
showBoundary(`${error}`);
return null;
}