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

gatsby-plugin-build-date

v1.0.0

Published

Gatsby plugin to add build date of site to internal GraphQL API

Downloads

1,438

Readme

gatsby-plugin-build-date

Add the date a Gatsby site was built to its internal GraphQL API as type currentBuildDate. The date is generated at build time, not runtime, so is baked into the site build.

It's common to see text like "Last Updated On 12/20/2019" in website footers - this plugin makes that date, reflecting when the site was built, available in the internal GraphQL API so that it's accessible in any component or page. No need to manually update the date, just build and let the new static date update itself.

This plugin is not designed to get "last build/update date/time" of specific files, it is only for when the whole site is compiled and gatsby-node.js is run.

Date formatting and localization is handled with the date-and-time library. Ideally could use toLocaleString() or toLocaleDateString() but Node.js does not include anything beyond "en-US" formatting by default.

Install

npm install --save gatsby-plugin-build-date or yarn add gatsby-plugin-build-date

How to Use

Date format defaults to a string in "en-US" format, e.g. "12/20/2019" if built on December 20, 2019. If this is acceptable and no further formatting or localization is needed, can include with no options:

// In your gatsby-config.js

module.exports = {
  plugins: [`gatsby-plugin-build-date`],
}

If different formatting or localization is desired, include with options:

// In your gatsby-config.js

module.exports = {
  plugins: [
    {
      resolve: `gatsby-plugin-build-date`,
      options: {
        formatAsDateString: true, // boolean, defaults to true - if false API will return unformatted string from new Date()
        formatting: {
          format: 'dddd D MMMM YYYY', // string, defaults to "MM/DD/YYYY" - pass in any acceptable date-and-time format
          utc: false, // boolean, defaults to false - output time as UTC or not, following date-and-time API
        },
        locale: 'fr', // string, defaults to null, which date-and-time defaults as "en" - whether to localize the date or not, can use any available date-and-time localization
      },
    },
  ],
}

The above configuration would return a date of "vendredi 20 décembre 2019" if built on December 20, 2019.

Options

The date-and-time library's date.format, date.utc, and date.locale methods are used to format dates when options are passed.

Available options, in the plugin's options object:

formatAsDateString: boolean, defaults to true. If set to false, no date formatting will be done, and the date added to the GraphQL API will be a string generated from new Date() at build time. Use this if it is preferred to format the date string on the client side, for example returned string of 2019-12-20T18:16:57.374Z could be formatted:

const buildDate = new Date(Date.parse(data.currentBuildDate.currentDate)).toLocaleDateString("de-DE")

in a component or page to produce "20.12.2019" string.

If formatAsDateString is true formatting and locale options can be passed.

formatting object contains two properties:

formatting.format: string, defaults to "MM/DD/YYYY". Accepts any valid format from date-and-time's API, reference Github page for full list of acceptable formats.

formatting.utc: boolean, defaults to false. For use with date-and-time's UTC option as documented in its API.

locale: string, defaults to null, which produces "en" locale. Accepts any valid date-and-time locale as listed here: https://github.com/knowledgecode/date-and-time/blob/HEAD/LOCALE.md

How to Query

Date string is added as type currentBuildDate to internal GraphQL API and can be queried as such:

{
  currentBuildDate {
    currentDate
  }
}

Use in a page query:

export const query = graphql`
  query {
    currentBuildDate {
      currentDate
    }
  }
`

or a static query:

const data = useStaticQuery(graphql`
  query {
    currentBuildDate {
      currentDate
    }
  }
`)

Note that if running in development mode with gatsby develop node APIs are not re-run during hot reload, so must quit and restart development mode if options are changed.

Acknowledgements

Date formatting and localization is done with KNOWLEDGECODE date-and-time library: https://github.com/knowledgecode/date-and-time - a great solution for formatting dates in Node.js without relying on full-icu library.