Inspired by Lee's blog, where every blog post is showing the number of views it has, I wanted to do something similar for my page. I'm also using Next.js 13 with the new app router but instead of storing the page views in a mysql database, I'll be using Upstash Redis.
Here's an example of what we'll be building. Each card on the home page will show the number of views it has.
Why Redis?
Redis already comes with 2 great commands that make it trivial to deduplicate and to increment a counter.
In order to get a more accurate counter, I want to debounce the incrementing of
the counter. If a user refreshes the page, the counter should only be
incremented once. We can do this really easily with Redis' SET
command. It has
a NX
option that will only set the key if it doesn't exist yet and an EX
option that will expire the key after a given amount of seconds. By combining
both of these options we can ensure a single user does not increment the counter
multiple times within a given timeframe.
The second command is INCR
which will increment a given key by 1 atomically.
Setting up Redis
Setting up a database on Upstash is easy and includes 10k requests per day
for free! You can create a new database
here. It literally takes only a few
seconds. Afterwards scroll down and copy the REST
connection
secrets to your .env.local
file:
Next.js
Now that we have a Redis database, we can start implementing the counter. We need to install @upstash/redis first:
In order to store the page views, we need two components. An api route and a client component. Let's start with the api route.
/api/incr.ts
Upstash and @upstash/redis
is compatible with Vercel's edge functions, so
first of all, we will import everything we need, setup redis and configure the
runtime to be edge
.
Create a new file /api/incr.ts
and add the following code:
Next up we'll require a slug or similar identifier to be passed in the request
body. If it's not present, we'll return a 400
status code.
Then we also need to get the user's IP address. We can do this by using the
req.ip
property. We'll hash the IP address using the SHA-256
algorithm and
store it in the database. This way we don't have to store the IP address
directly, which could be a security concern.
Now let's use the first redis command mentioned above. Using SET
together with
NX
and EX
gives us an easy way to check if a specific ip address has been
viewing a page within the last 24 hours:
The last thing to do is to increment the counter for the given slug. We'll use
the INCR
command for this:
For reference, you can find the complete code here
/app/[slug]/view.tsx
Next, let's create a small client component, that sends a request to the api route we just created whenever it mounts. This component can then be embedded in any page we want to track.
/app/[slug]/page.tsx
The last thing we need to do is to add the ReportView
component to the page we want to track:
From now on all visits to /app/[slug]
will be tracked and the counter will be incremented for each unique visitor in the last 24h.
Displaying Views
Tracking the views is nice, but we also want to display them publicly. Let's see how we can do that.
In order to display the number of views, we need to look them up from the database. We can do this by using the GET
command. We should also add a revalidate
config to the page component, so that the page is revalidated every 60 seconds and not for every request.
Final words
Check out a full example of this at chronark.com. The code is available on GitHub. Specifically, here are the relevant bits:
If you're interested in more page view analytics, please let us know on Twitter or Discord.