npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

next-gitinfo

v1.1.0

Published

<div align="center"> <h1><code>next-gitinfo</code></h1>

Downloads

4

Readme

Version BSD-3-Clause License Downloads Bundle size

Build status Dependencies Maintainability

Code of Conduct PRs Welcome

Watch on GitHub Star on GitHub Tweet

Contents

Motivation

There are lots of reasons to want to know when and why a page was changed. Perhaps your Next site serves a Wiki, updateable through Git. Maybe you want to create a /version page to keep track of what has been deployed in each environment. Maybe you use Markdown files for your blog articles, and want to show the last time that an article was updated.

No matter what your reason, having access to the Git information for a specific page in your Next.js page can be extremely valuable, though getting access to that info is a serious pain-in-the-butt.

This library provides Git information for every page in your Next app to be used however you see fit. It also provides hooks to easily access that information in each page.

Installation

npm install --save-dev next-gitinfo
# or
yarn add --dev next-gitinfo

Usage

withGitInfo

The withGitInfo library extends your Next config to include Git information in your builds. To use it, you must install it in your next.config.js file:

// Module imports
const withGitInfo = require('next-gitinfo')

module.exports = withGitInfo()

Alternatively, if you already have other Next configuration, you can pass it directly into withGitInfo:

// Module imports
const withGitInfo = require('next-gitinfo')

module.exports = withGitInfo({
	env: {},
	webpack: config => {
		return config
	},
})

Restarting your Next server will yield a new process.env.gitInfo variable that is available across your entire application.

// pages/version.js

export default VersionPage() {
	return (
		<pre>
			{JSON.stringify(process.env.gitInfo, null, 2)}
		</pre>
	)
}

useCurrentPageGitInfo

The useCurrentPageGitInfo hook allows you to retrieve the git info for the current page. This even works for dynamic pages.

// pages/index.js
import { useCurrentPageGitInfo } from 'next-gitinfo/hooks'

export default VersionPage() {
	const gitInfo = useCurrentPageGitInfo()
	return (
		<pre>
			{JSON.stringify(gitInfo, null, 2)}
		</pre>
	)
}

How it works

When starting your Next app, the withGitInfo library walks your pages/ directory. For each page file that it finds, it will get a raw Git commit and parse that commit into a readable object. That object is then injected into the app as process.env.gitInfo.

To get Git info for current page, the useCurrentPageGitInfo hook uses the Next.js router to determine the page currently being viewed, then pulls the data from process.env.gitInfo.pages for that page.

NOTE: withGitInfo only runs at compile time. This means that if you commit a file, process.env.gitInfo will not be updated until you restart the Next server.