Fixing the Blog - Adding View Count to the Blog
- front
- blog
Introduction
Previously, while creating the blog, I had added a view count counter in Creating a Blog - 11. Adding View Count. However, due to numerous errors, I had removed it. I will recreate it now.
I referenced the article Adding a View Counter to your Next.js Blog.
Upstash Redis offers 10,000 free usage requests per day, and since it operates on a NoSQL key-value basis, I deemed it suitable for a view count counter. Additionally, as this is an article from Upstash's official blog, I assessed its reliability to be high.
1. Basic Setup
First, log in to Upstash (I used Google login) and create a new database in the Upstash Console. I named it witch-blog-view-counter
and selected the Japanese region, which seemed to be the closest.
I enabled traffic SSL encryption and the setting to reject requests that exceed the max size.
Then, add the REST connection information from the DB dashboard into your .env.local
file.
Next, install the Redis package. This completes the basic setup.
2. View Count Increment Logic
Let's create a route handler to implement the logic for incrementing the view count. Create app/viewcount/route.ts
to accept POST requests at /viewcount
.
Redis.fromEnv()
automatically reads UPSTASH_REDIS_REST_URL
and UPSTASH_REDIS_REST_TOKEN
from environment variables to instantiate a Redis client.
This function retrieves slug
from the request body and increments the value of the key pageviews:projects:${slug}
by 1. The incr
command initializes the key to 0 if it does not exist and then adds 1.
2.1. Preventing Duplicates
Currently, there is no handling for duplicate requests in the view count increment logic. To address this, we will prevent duplicate requests based on the user's IP address.
This can be implemented using either request.ip
or the X-Forwarded-For
header. Although users may manipulate this, it suffices for our needs as we are not seeking extreme accuracy in managing the view count.
However, storing the IP address directly could pose security issues, so we will hash and store it instead. We can use the crypto
package for this. The hash value of the IP can be generated as follows.
Next, we will create a key that starts with deduplicate:
and send a set
request to store a value of true
. The key should be retained for 60 * 60 seconds (1 hour) and should only have its value written if it expires. This can be accomplished using the NX
option and the EX
expiration time option.
Moreover, the view count should only increment if there is no duplicate IP or if the IP is absent. The complete implementation of the POST
handler is as follows.
2.2. View Count Increment Component
Now, let's create a component to handle the view count increment task. The component named ViewReporter
will accept slug
as a prop. As we will use the useEffect
hook, it should be defined as a client component and return null
since there will be no rendering.
3. Applying the View Count Counter
Next, where should we check the view count and for what? Initially, I will display the total number of visitors on the homepage and show the view counts for each post.
3.1. Total Visits
To aggregate the total visits to the blog, we apply ViewReporter
in the root layout of Next.js. The slug is set to witch-blog:total-views
.
3.2. Displaying Total Visits
Next, let's create a simple component for displaying the total visits, called ViewCounter
.
On the homepage, we will fetch the view count for the slug corresponding to the total visitors and display it. We will also set the route cache revalidation time to 60 seconds.
3.3. Post Views
Now that we have the component to show the content of the blog posts located in app/posts/[slug]/page.tsx
, we just need to increment the view count for the corresponding [slug]
and display it.
In the future, we could consider adding functionality for sorting posts by popularity or displaying posts with the highest view counts once we accumulate more data based on the database.
References
Adding a View Counter to your Next.js Blog
Nextjs Route Handlers Official Documentation
NextRequest Official Documentation
Step-by-Step Blog Creation - Counting Views