You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
subtitle: Use case for KV store, Build a blog page view counter.
lede: Let's build together a simple page view counter for every Astro blog page we have, using the new KV store by Vercel.
So I decided to add a page view counter for my blog, one and easy to implement this is to use KV stores. It also coincides with Vercel Ship event which was held between May 1 & May 5, on their first day, they announced their data storage solutions, so I wanted to try their new KV storage solution.
KV Store
A KV store is a simple database that stores data as key-value pairs. You can quickly store and retrieve values using unique keys. It's like having a box where you put things and label them with keys, so you can easily find and access them later.
For example:
"Name" => "John"
"Age" => 25
"City" => "New York"
We can retrieve values using their respective keys.
Setting up data
Now, let's decide on the format of the key => value data, so we know that each post has a unique slug, we can use that as an id for the post, so the format would be something like this: post:slug => { "views": value }.
In redis terms, this will translate to a hash, the redis command to set it would be like this:
HSET post:slug views 0
or in our case if we want to increment it, we use the following:
HINCRBY post:slug views 1
HSET: create a new hash with the provided fields values
HINCRBY: create a new hash if there's none, or increment hash field value by the provided value
post:slug: hash key
views: hash field
Building KV store (Vercel)
Now knowing the format of the data, we can start first by creating a new KV store in Vercel dashboard:
Next, we link our database to the project, and then, we copy ENV variables and paste them in our .env file in the root of our project:
In order to change the values in KV store, we need to create an endpoint in Astro, so when we call that endpoint, we make the change to the KV store.
So first, as endpoints are not supported in static output mode, we need to change that to server output mode and use a 3rd party adapter, in our case it's Vercel, so we need to do the following changes:
Now we'll have a new endpoint: GET /api/view/[slug].
Triggering the endpoint
So now after setting up everything, we need to find a way to trigger and call that endpoint, we can make a simple fetch call to that endpoint, or if we don't want to use javascript, we can use this little trick: use <img> tag to load the endpoint url instead, it will be something like the following:
And we put that piece of code inside the blog post Astro template, now whenever a blog post renders, it will automatically make a GET request to our endpoint.
Personally, I find this method very elegant, but it may cause some issues with other browsers, it needs to be tested heavily, we don't want certain browsers not counting views, we cross check views on analytics to see if some browser views are not matching our views counter in our KV store.
Troubleshooting
Currently we may have a problem using endpoints with Vercel SDK on Astro default dev server, instead, we need to use Vercel dev server in order to make use of their KV Store SDK, we just need to run: vercel dev..
We may also have to install the required dependencies if any errors are thrown.
Hope you found this helpful, and as always stay tuned to some more content ✌️
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
subtitle: Use case for KV store, Build a blog page view counter.
lede: Let's build together a simple page view counter for every Astro blog page we have, using the new KV store by Vercel.
So I decided to add a page view counter for my blog, one and easy to implement this is to use KV stores. It also coincides with Vercel Ship event which was held between May 1 & May 5, on their first day, they announced their data storage solutions, so I wanted to try their new KV storage solution.
KV Store
A KV store is a simple database that stores data as key-value pairs. You can quickly store and retrieve values using unique keys. It's like having a box where you put things and label them with keys, so you can easily find and access them later.
For example:
We can retrieve values using their respective keys.
Setting up data
Now, let's decide on the format of the
key => valuedata, so we know that each post has a unique slug, we can use that as an id for the post, so the format would be something like this:post:slug => { "views": value }.In redis terms, this will translate to a hash, the redis command to set it would be like this:
or in our case if we want to increment it, we use the following:
HSET: create a new hash with the provided fields valuesHINCRBY: create a new hash if there's none, or increment hash field value by the provided valuepost:slug: hash keyviews: hash fieldBuilding KV store (Vercel)
Now knowing the format of the data, we can start first by creating a new KV store in Vercel dashboard:
Next, we link our database to the project, and then, we copy ENV variables and paste them in our
.envfile in the root of our project:That's it!
Creating endpoint
In order to change the values in KV store, we need to create an endpoint in Astro, so when we call that endpoint, we make the change to the KV store.
So first, as endpoints are not supported in
staticoutput mode, we need to change that toserveroutput mode and use a 3rd party adapter, in our case it's Vercel, so we need to do the following changes:Now we install the Vercel KV SDK package:
Next thing, we need to add this line:
export const prerender = true;on the top of each page file that use static generation,Then, we create a new endpoint file under
/src/pages/api/view/, named:[slug].tswith the following content:Now we'll have a new endpoint:
GET /api/view/[slug].Triggering the endpoint
So now after setting up everything, we need to find a way to trigger and call that endpoint, we can make a simple fetch call to that endpoint, or if we don't want to use javascript, we can use this little trick: use
<img>tag to load the endpoint url instead, it will be something like the following:And we put that piece of code inside the blog post Astro template, now whenever a blog post renders, it will automatically make a
GETrequest to our endpoint.Personally, I find this method very elegant, but it may cause some issues with other browsers, it needs to be tested heavily, we don't want certain browsers not counting views, we cross check views on analytics to see if some browser views are not matching our views counter in our KV store.
Troubleshooting
Currently we may have a problem using endpoints with Vercel SDK on Astro default dev server, instead, we need to use Vercel dev server in order to make use of their KV Store SDK, we just need to run:
vercel dev..We may also have to install the required dependencies if any errors are thrown.
Hope you found this helpful, and as always stay tuned to some more content ✌️
Beta Was this translation helpful? Give feedback.
All reactions