ViewsCounter Issue
j471n opened this issue · comments
As I went through your code, I saw a bug (as per my preference). In your api/views/[slug]
you are adding views to the database when your request method is POST
. It's all good till here.
Problem
Let's assume you are in the development phase or making something new style or something else, when you visit the blog slug
it will increase the view to the database even if you are in the development phase and that view is not genuine it's just you who were just making some changes.
Solution
You can fix this by just following the method-
# on line 11 in `api/views/[slug]`
- if (req.method === 'POST') {
+ if (req.method === "POST" && process.env.NODE_ENV === "production") {
It'll just check if the application is in the production
only then increase the views of the blog.
I use a different database for local/preview to prevent this. I still like to check that the blog views increment, just not for production counts 👍 In the future I'll probably do a database branch for this.