Giter Club home page Giter Club logo

hits's Introduction

Hits

Hit count Code style License

A no-nonsense hit count badge generator built with Next.js and FaunaDB.

Usage

To use the generated hit count badge, add the following snippet to your readme file.

![Hit count](https://hits.vercel.app/USER/REPO.svg)

Running locally

# Install dependencies
npm install

# Run local development server on https://localhost:3000/
npm run dev

Deployment instructions

  1. Firstly, you'll need to set up the back-end database to store the hit counts.

    1. Head over to https://fauna.org and create an account.

    2. Create a database (Select US as the region - if your region of choice is different, you may need to change the db connection domain over at ./pages/api/[...path].js).

    3. Within the created database, create a collection.

    4. Next, click Indexes on the sidebar and add an index.

      • Name: by_path
      • Terms: data.path
      • Values: data.count
      • Unique: Checked
      • Serialized: Checked
    5. After that, click Functions and add a function with the name getHits. For the function body, copy and paste the below function:

      Query(
        Lambda(
          "path",
          Let(
            { match: Match(Index("by_path"), Var("path")) },
            If(
              Exists(Var("match")),
              Let(
                { doc: Get(Var("match")) },
                Update(Select("ref", Var("doc")), {
                  data: { count: Add(Select(["data", "count"], Var("doc")), 1) }
                })
              ),
              Create(Collection("hitcount"), {
                data: { path: Var("path"), count: 1 }
              })
            )
          )
        )
      )
      

      This function takes a single argument (the path), and adds it to the database if it does not exist, or increments the count if it does.

    6. Finally, hit Security in the sidebar and generate a server secret.

  2. Set up the front-end:

    To deploy the Next.js app, you can use a platform like Netlify or Vercel. Add the FaunaDB server secret as an environment variable called FAUNADB_SECRET.

    Deploy with Vercel

License

MIT

hits's People

Contributors

radiantly avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.