Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Adding view count to your Nextjs Blog
Navin Kodag
Navin Kodag

Posted on

     

Adding view count to your Nextjs Blog

Alright lads, this will be a quick one. I want to add the views count functionality on my personal portfolio website's blog section.

ready.gif

Expected behavior:

  • Blogs Page : List of blogs -> Show count.
  • Blog Page : Particular Article -> Show count and Increment count.

How to achieve:

  • Use supabase to store count by slug
  • Stored procedure to increment count

Tools that I'll need:

  • supabase : open source firebase alternative
  • swr : data fetching

Setting up supabase table :

Create a tableviews with schema like such:

  • slug -> text -> primary key
  • created_at -> timestamp -> now()
  • count -> int2

Updatingcount:

  • Fetch count
  • Increment one
  • Fetch count again

Now we can reduce this to one db call using stored procedures:
stored-procedure.png

createfunctionincrement(slug_texttext)returnsvoidas$$updateviewssetcount=count+1whereslug=slug_text;$$languagesqlvolatile;
Enter fullscreen modeExit fullscreen mode

In NextJs:

We'll define a route for ease:
- /api/view/{slug}
and then we'll use thePOST request to register a view andGET to increment the view count.
Our handler code will look like this:
views.ts

import{createClient,PostgrestError}from"@supabase/supabase-js";constsupabase=createClient(process.env.SUPABASE_URL,process.env.SUPABASE_KEY);interfaceSupabaseResult{data?:{count:number};error?:PostgrestError;}///constgetViews=async(slug:string):Promise<number>=>{const{data:views,error}:SupabaseResult=awaitsupabase.from("views").select(`count`).match({slug:slug}).single();if(error&&error.details.includes(`0 rows`)){const{data,error}:SupabaseResult=awaitsupabase.from(`views`).insert({slug:slug,count:1},{returning:`representation`}).single();returndata.count;}if(!views){return0;}returnviews.count;};///constregisterView=async(slug:string):Promise<void>=>{const{data,error}=awaitsupabase.rpc("increment",{slug_text:slug,});};export{getViews,registerView};
Enter fullscreen modeExit fullscreen mode
  • /api/view/[slug].ts
// /api/view/[slug].ts// Next.js API route support: https://nextjs.org/docs/api-routes/introductionimport{getViews,registerView}from"lib/views";importtype{NextApiRequest,NextApiResponse}from"next";interfaceData{message?:string;status?:number;count?:number;}///exportdefaultasyncfunctionhandler(req:NextApiRequest,res:NextApiResponse<Data>):Promise<void>{constslug=req.query.slug.toString();///if(!slug){returnres.status(400).json({message:`invalid slug`});}if(req.method==`POST`){awaitregisterView(slug);}constcount=awaitgetViews(slug);returnres.status(200).json({count:count});}
Enter fullscreen modeExit fullscreen mode

ViewCounter Component

  • view_counter.tsx
importfetcherfrom"lib/fetcher";import{Views}from"lib/types";import{useEffect}from"react";importuseSWRfrom"swr";interfaceProps{slug:string;}constViewCounter=({slug}:Props)=>{const{data}=useSWR<Views>(`/api/views/${slug}`,fetcher);useEffect(()=>{constregisterView=()=>fetch(`/api/views/${slug}`,{method:"POST",});registerView();},[slug]);return(<span>{`${(data?.count??0)>0?data.count.toLocaleString():"–––"} views`}</span>);};exportdefaultViewCounter;
Enter fullscreen modeExit fullscreen mode

Our views in action:
view_counter.png
view_count.png

The code of this project lives at :https://github.com/100lvlmaster/100lvlmaster.in


You can find me at:https://100lvlmaster.in

Top comments(2)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
razzusharma profile image
Raju Sharma Dahal
  • Joined

Cant i do the same thing without database or any other simple orm services?

CollapseExpand
 
100lvlmaster profile image
Navin Kodag
Full stack developer based in Navi Mumbai, India.

yes

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Full stack developer based in Navi Mumbai, India.
  • Location
    Navi Mumbai, India.
  • Joined

More fromNavin Kodag

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp