diff --git a/images/avatar.jpg b/images/avatar.jpg deleted file mode 100644 index 69da449..0000000 Binary files a/images/avatar.jpg and /dev/null differ diff --git a/src/components/Views.tsx b/src/components/Views.tsx index 3dbb26a..749898b 100644 --- a/src/components/Views.tsx +++ b/src/components/Views.tsx @@ -1,33 +1,27 @@ import {ElementType, useEffect} from 'react' -import useSWR from 'swr' +import useSWRImmutable from 'swr/immutable' import fetcher from '@/lib/fetcher' + function numberFormat(value: number) { return new Intl.NumberFormat('en', { notation: 'compact' }).format(value) } -type ViewsType = { - views: string -} +const updateViews = (slug: string) => fetcher(`/api/views/${slug}`, {method: 'POST'}) export function Views({as: Component = 'span', slug}: { as?: ElementType, slug: string }) { - const {data} = useSWR(`/api/views/${slug}`, fetcher) + const {data} = useSWRImmutable(`/api/views/${slug}`, fetcher) const views = Number(data?.views) useEffect(() => { - const registerView = () => - fetch(`/api/views/${slug}`, { - method: 'POST' - }) - - registerView().then(r => r) + updateViews(slug).then(r => r) }, [slug]) return ( - {views > 0 ? ` · ${numberFormat(views)} views` : ''} + {` · ${views > 0 ? numberFormat(views) : '---'} views`} ) } \ No newline at end of file