GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - GuiDevloper/nullstack-view-counter: Nullstack example of counting views and avoiding requests from non-user sources

BranchesTags
Open more actions menu

Latest commit

 

History

9 Commits
9 Commits

Folders and files

NameName
Last commit message
Last commit date
api
api
 
 
public
public
 
 
src
src
 
 
.env.example
.env.example
 
 
.eslintrc.js
.eslintrc.js
 
 
.gitignore
.gitignore
 
 
README.md
README.md
 
 
client.ts
client.ts
 
 
package.json
package.json
 
 
pnpm-lock.yaml
pnpm-lock.yaml
 
 
server.ts
server.ts
 
 
tsconfig.json
tsconfig.json
 
 
vercel.json
vercel.json
 
 

Repository files navigation

nullstack-view-counter

Nullstack example of counting views and avoiding requests from non-user sources.

Reasoning

Nullstack in production environment by default loads the page and activate a service-worker that loads again, making double-run at the first user visit (calling any initial server function too).

Also many bots (like VercelBot) requests the page, making more runs (and logs).

This example caches the root page /, and also checks the user-agent from requests, trying to count only real users visits.

  • In server.ts all get requests are intercepted and cached for 3s (enough for worker) - Comment line 12 for the initial request double-run again.

  • In src/ViewCount.tsx the logic happens. A server variable stores the count - Without the above caching, the initial goes wrong, pressing Sync with server shows how the server got hit twice.

  • In src/utils.ts there's a method to check if the user agent isn't among known bots, blocking the server count for them.

How to run this project

  1. Install the dependencies: npm install

  2. Rename the .env.example file to .env

  3. Run the app in production mode:

npm run build && node .production/server.js
  1. Open http://localhost:3000 in a anonymous/zero-cache tab.

  2. Check the terminal and view.

💡 Vercel Serverless instances does have a temporary state being a good environment to test this behavior in production and storing logs.

Learn more

Read Nullstack documentation

About

Nullstack example of counting views and avoiding requests from non-user sources

nullstack-view-counter.vercel.app

Topics

nullstack

Resources

Readme
Activity

Stars

0 stars

Watchers

1 watching

Forks

0 forks
Report repository

Used by

Contributors

Languages

You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.