From ed7f863a12c95f8c4816aeba2b0d3c326b6c694f Mon Sep 17 00:00:00 2001 From: Alvis HT Tang Date: Thu, 15 Jul 2021 23:32:29 +0100 Subject: [PATCH] fix: ignore cache if last_edit_time is within the last minute see https://developers.notion.com/changelog/last-edited-time-is-now-rounded-to-the-nearest-minute --- source/client.ts | 9 ++++++++- source/node.ts | 14 ++++++-------- spec/node.spec.ts | 2 +- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/source/client.ts b/source/client.ts index ca04bd3..cfb9c54 100644 --- a/source/client.ts +++ b/source/client.ts @@ -71,6 +71,8 @@ export const DEFAULT_TTL: NotionTTL = { pageContent: 0, }; +const ONE_MINUTE = 60000; + /** A simple Notion client */ export class Notion { private cache: Cache; @@ -277,7 +279,12 @@ export class Notion { const cacheKey = `page:${page.id}:content`; const cachedPage = await this.cache.get(cacheKey); - if (cachedPage && cachedPage.last_edited_time === page.last_edited_time) { + if ( + cachedPage && + cachedPage.last_edited_time === page.last_edited_time && + // don't use the cache if the last edited time happened to be the last minute since Notion rounded the time resolution to minute level recently + Date.now() - new Date(page.last_edited_time).getTime() > ONE_MINUTE + ) { return cachedPage; } else { const normalisedPage = await this.normalisePage(page); diff --git a/source/node.ts b/source/node.ts index 628eadb..8082a5c 100644 --- a/source/node.ts +++ b/source/node.ts @@ -13,6 +13,8 @@ * ------------------------------------------------------------------------- */ +import { createHash } from 'crypto'; + import { name } from '#.'; import type { NodeInput, NodePluginArgs } from 'gatsby'; @@ -39,6 +41,7 @@ type NormalisedEntity = E extends any ? Omit & { parent: Link | null; children: Link[]; + digest: string; } : never; @@ -189,10 +192,7 @@ export class NodeManager { this.createNodeId(`${object}:${id}`), ), internal: { - contentDigest: this.createContentDigest({ - id: entity.id, - lastEditedTime: entity.last_edited_time, - }), + contentDigest: entity.digest, ...internal, }, }; @@ -254,10 +254,7 @@ export class NodeManager { for (const [id, newEntity] of newMap.entries()) { const oldEntity = oldMap.get(id); - if ( - oldEntity && - oldEntity.last_edited_time !== newEntity.last_edited_time - ) { + if (oldEntity && oldEntity.digest !== newEntity.digest) { updated.push(newEntity); } } @@ -298,6 +295,7 @@ export function computeEntityMap( ...entity, parent: normaliseParent(entity.parent), children: [], + digest: createHash('sha256').update(JSON.stringify(entity)).digest('hex'), }); } diff --git a/spec/node.spec.ts b/spec/node.spec.ts index 6bc9198..731f5b8 100644 --- a/spec/node.spec.ts +++ b/spec/node.spec.ts @@ -241,7 +241,7 @@ describe('cl:NodeManager', () => { ], }); await manager.update([updatedDatabase, ...updatedDatabase.pages]); - expect(createNode).toBeCalledTimes(1); + expect(createNode).toBeCalledTimes(2); expect(deleteNode).toBeCalledTimes(1); }); });