From 446f9ff91e61b6546ad26702b102e7c572e2e2d7 Mon Sep 17 00:00:00 2001 From: Josh Johnson Date: Tue, 22 Feb 2022 17:42:09 -0500 Subject: [PATCH] chore(gatsby): Cache date formatting in lmdb cache (#34834) Co-authored-by: Michal Piechowiak Co-authored-by: gatsbybot --- .../src/query/__tests__/data-tracking.js | 7 +++ packages/gatsby/src/schema/types/date.ts | 43 ++++++++++++++++--- 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/packages/gatsby/src/query/__tests__/data-tracking.js b/packages/gatsby/src/query/__tests__/data-tracking.js index ccf6de2c16f8e..bbf8b203e116d 100644 --- a/packages/gatsby/src/query/__tests__/data-tracking.js +++ b/packages/gatsby/src/query/__tests__/data-tracking.js @@ -35,6 +35,13 @@ jest.mock(`fs-extra`, () => { } }) +jest.mock(`gatsby-telemetry`, () => { + return { + trackCli: () => {}, + captureEvent: () => {}, + } +}) + jest.mock(`../../utils/cache-lmdb`, () => { return { default: class MockedCache { diff --git a/packages/gatsby/src/schema/types/date.ts b/packages/gatsby/src/schema/types/date.ts index ae416ce655f33..47e884aa9e994 100644 --- a/packages/gatsby/src/schema/types/date.ts +++ b/packages/gatsby/src/schema/types/date.ts @@ -1,6 +1,7 @@ import moment, { MomentInput, unitOfTime, LocaleSpecifier } from "moment" import { GraphQLScalarType, Kind, GraphQLFieldConfig } from "graphql" import { oneLine } from "common-tags" +import GatsbyCacheLmdb from "../../utils/cache-lmdb" interface IFormatDateArgs { date: Date | string @@ -209,19 +210,40 @@ export function isDate(value: MomentInput): boolean { return typeof value !== `number` && momentDate.isValid() } -const formatDate = ({ +let formatDateCache: GatsbyCacheLmdb | undefined +function getFormatDateCache(): GatsbyCacheLmdb { + if (!formatDateCache) { + formatDateCache = new GatsbyCacheLmdb({ + name: `format-date-cache`, + encoding: `string`, + }).init() + } + return formatDateCache +} + +const formatDate = async ({ date, fromNow, difference, formatString, locale = `en`, -}: IFormatDateArgs): string | number => { +}: IFormatDateArgs): Promise => { const normalizedDate = JSON.parse(JSON.stringify(date)) if (formatString) { - return moment + const cacheKey = `${normalizedDate}-${formatString}-${locale}` + const cachedFormat = await getFormatDateCache().get(cacheKey) + if (cachedFormat) { + return cachedFormat as string + } + + const result = moment .utc(normalizedDate, ISO_8601_FORMAT, true) .locale(locale) .format(formatString) + + await getFormatDateCache().set(cacheKey, result) + + return result } else if (fromNow) { return moment .utc(normalizedDate, ISO_8601_FORMAT, true) @@ -282,11 +304,18 @@ export const getDateResolver = ( from: options.from || info.from, fromNode: options.from ? options.fromNode : info.fromNode, }) - if (date == null) return null - return Array.isArray(date) - ? date.map(d => formatDate({ date: d, ...args })) - : formatDate({ date, ...args }) + if (date == null) { + return null + } + + if (Array.isArray(date)) { + return await Promise.all( + date.map(d => formatDate({ date: d, ...args })) + ) + } + + return await formatDate({ date, ...args }) }, } }