Gatsby Plugin Build Date
This Gatsby plugin, currently at version 1.0.0, provides a utility for injecting the site's build timestamp into the internal GraphQL API under the `currentBuildDate` type. It is designed to make the site's last build date accessible within components and pages, useful for displaying 'Last Updated' footers. The date is generated statically at build time, ensuring consistency across deployments. A key differentiator is its integration with the `date-and-time` library for robust date formatting and localization, which circumvents Node.js's default `toLocaleString()` limitations that typically only support 'en-US'. The plugin's release cadence is generally slow for a utility of this nature, with releases primarily focusing on stability or compatibility with new Gatsby major versions, rather than frequent feature additions. It explicitly clarifies that it's for the overall site build date, not individual file modification times.
Common errors
-
Cannot query field `currentBuildDate` on type `Query`
cause The plugin `gatsby-plugin-build-date` has not been correctly included in `gatsby-config.js`, or the Gatsby development server has not been restarted after configuration changes.fixEnsure `gatsby-plugin-build-date` is listed in the `plugins` array in `gatsby-config.js` (either as a string or an object with `resolve`). Then, restart your Gatsby development server (`gatsby develop`) or rebuild your site (`gatsby build`). -
Date returned from GraphQL is in the wrong format or locale.
cause Incorrect or missing `formatting` and `locale` options in `gatsby-config.js` for the plugin, or `formatAsDateString` is set to `false`, returning an unformatted ISO string.fixReview the `options` object for `gatsby-plugin-build-date` in `gatsby-config.js`. Ensure `formatAsDateString: true` and correctly configure `formatting.format` (e.g., 'DD/MM/YYYY') and `locale` (e.g., 'es') according to the `date-and-time` library documentation. Remember to rebuild or restart `gatsby develop`. -
I want to display when a specific blog post or page was last updated, but `currentBuildDate` shows the site build date.
cause Misunderstanding the plugin's intended scope. `gatsby-plugin-build-date` provides a *site-wide* build timestamp, not specific file modification dates.fixFor file-specific last modified dates, consider using other Gatsby plugins that integrate with your data source (e.g., `gatsby-source-filesystem` with `gatsby-transformer-remark` that can read frontmatter `date` fields, or a headless CMS that provides update timestamps). This plugin is not suitable for individual content updates.
Warnings
- gotcha This plugin is designed to provide the *site's overall build timestamp*, not the last modified date of individual files (e.g., blog posts or images). Developers often misinterpret its scope.
- gotcha Node.js environments often have limited locale data, defaulting `toLocaleString()` to 'en-US'. This plugin uses the `date-and-time` library to offer robust formatting and localization, so developers should use the plugin's `formatting` and `locale` options instead of client-side `toLocaleString` unless `formatAsDateString` is explicitly set to `false`.
- gotcha Without explicit `formatting` options, the date will default to 'MM/DD/YYYY' (e.g., '12/20/2019'). This might not be suitable for all international users or regional date formats.
- gotcha If `formatAsDateString` is set to `false`, the GraphQL API will return an unformatted ISO string from `new Date()` (e.g., `2019-12-20T18:16:57.374Z`). This requires client-side parsing and formatting, which can be unexpected if a pre-formatted string was anticipated.
- breaking This plugin explicitly targets Gatsby `^2.0.0` as a peer dependency. While it may function with newer major versions of Gatsby (v3, v4, v5+), compatibility is not guaranteed, and potential breaking changes in Gatsby's core APIs might affect its operation.
Install
-
npm install gatsby-plugin-build-date -
yarn add gatsby-plugin-build-date -
pnpm add gatsby-plugin-build-date
Imports
- `gatsby-plugin-build-date` (minimal config)
import gatsbyPluginBuildDate from 'gatsby-plugin-build-date'; // Not imported directly in user code
// In gatsby-config.js module.exports = { plugins: [ `gatsby-plugin-build-date` ] } - `gatsby-plugin-build-date` (with options)
// In gatsby-config.js module.exports = { plugins: [ { resolve: `gatsby-plugin-build-date`, options: { formatAsDateString: true, formatting: { format: 'dddd D MMMM YYYY', utc: false }, locale: 'fr' } } ] } - `currentBuildDate` (GraphQL query)
query MySiteBuildDate { currentBuildDate { currentDate } }
Quickstart
// In your gatsby-config.js
module.exports = {
plugins: [
{
resolve: `gatsby-plugin-build-date`,
options: {
formatAsDateString: true, // boolean, defaults to true
formatting: {
format: 'YYYY-MM-DD HH:mm:ss', // ISO-like format with time
utc: true // output time as UTC
},
locale: 'en' // default locale
}
},
],
}
// In a Gatsby component (e.g., in a page query or static query):
// Assuming 'data' prop is available from a page query
// or 'useStaticQuery' hook
/*
import { graphql, useStaticQuery } from 'gatsby';
function Footer() {
const data = useStaticQuery(graphql`
query BuildDateQuery {
currentBuildDate {
currentDate
}
}
`);
return (
<footer>
Last Built: {data.currentBuildDate.currentDate} UTC
</footer>
);
}
export default Footer;
*/