Skip to content

Commit 446f9ff

Browse files
imjoshinpiehgatsbybot
authored
chore(gatsby): Cache date formatting in lmdb cache (#34834)
Co-authored-by: Michal Piechowiak <[email protected]> Co-authored-by: gatsbybot <[email protected]>
1 parent d4f0cef commit 446f9ff

File tree

2 files changed

+43
-7
lines changed

2 files changed

+43
-7
lines changed

packages/gatsby/src/query/__tests__/data-tracking.js

+7
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ jest.mock(`fs-extra`, () => {
3535
}
3636
})
3737

38+
jest.mock(`gatsby-telemetry`, () => {
39+
return {
40+
trackCli: () => {},
41+
captureEvent: () => {},
42+
}
43+
})
44+
3845
jest.mock(`../../utils/cache-lmdb`, () => {
3946
return {
4047
default: class MockedCache {

packages/gatsby/src/schema/types/date.ts

+36-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import moment, { MomentInput, unitOfTime, LocaleSpecifier } from "moment"
22
import { GraphQLScalarType, Kind, GraphQLFieldConfig } from "graphql"
33
import { oneLine } from "common-tags"
4+
import GatsbyCacheLmdb from "../../utils/cache-lmdb"
45

56
interface IFormatDateArgs {
67
date: Date | string
@@ -209,19 +210,40 @@ export function isDate(value: MomentInput): boolean {
209210
return typeof value !== `number` && momentDate.isValid()
210211
}
211212

212-
const formatDate = ({
213+
let formatDateCache: GatsbyCacheLmdb | undefined
214+
function getFormatDateCache(): GatsbyCacheLmdb {
215+
if (!formatDateCache) {
216+
formatDateCache = new GatsbyCacheLmdb({
217+
name: `format-date-cache`,
218+
encoding: `string`,
219+
}).init()
220+
}
221+
return formatDateCache
222+
}
223+
224+
const formatDate = async ({
213225
date,
214226
fromNow,
215227
difference,
216228
formatString,
217229
locale = `en`,
218-
}: IFormatDateArgs): string | number => {
230+
}: IFormatDateArgs): Promise<string | number> => {
219231
const normalizedDate = JSON.parse(JSON.stringify(date))
220232
if (formatString) {
221-
return moment
233+
const cacheKey = `${normalizedDate}-${formatString}-${locale}`
234+
const cachedFormat = await getFormatDateCache().get(cacheKey)
235+
if (cachedFormat) {
236+
return cachedFormat as string
237+
}
238+
239+
const result = moment
222240
.utc(normalizedDate, ISO_8601_FORMAT, true)
223241
.locale(locale)
224242
.format(formatString)
243+
244+
await getFormatDateCache().set(cacheKey, result)
245+
246+
return result
225247
} else if (fromNow) {
226248
return moment
227249
.utc(normalizedDate, ISO_8601_FORMAT, true)
@@ -282,11 +304,18 @@ export const getDateResolver = (
282304
from: options.from || info.from,
283305
fromNode: options.from ? options.fromNode : info.fromNode,
284306
})
285-
if (date == null) return null
286307

287-
return Array.isArray(date)
288-
? date.map(d => formatDate({ date: d, ...args }))
289-
: formatDate({ date, ...args })
308+
if (date == null) {
309+
return null
310+
}
311+
312+
if (Array.isArray(date)) {
313+
return await Promise.all(
314+
date.map(d => formatDate({ date: d, ...args }))
315+
)
316+
}
317+
318+
return await formatDate({ date, ...args })
290319
},
291320
}
292321
}

0 commit comments

Comments
 (0)