Skip to content

Commit ed7f863

Browse files
committed
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
1 parent 40f4e64 commit ed7f863

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

source/client.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ export const DEFAULT_TTL: NotionTTL = {
7171
pageContent: 0,
7272
};
7373

74+
const ONE_MINUTE = 60000;
75+
7476
/** A simple Notion client */
7577
export class Notion {
7678
private cache: Cache;
@@ -277,7 +279,12 @@ export class Notion {
277279
const cacheKey = `page:${page.id}:content`;
278280
const cachedPage = await this.cache.get<FullPage>(cacheKey);
279281

280-
if (cachedPage && cachedPage.last_edited_time === page.last_edited_time) {
282+
if (
283+
cachedPage &&
284+
cachedPage.last_edited_time === page.last_edited_time &&
285+
// 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
286+
Date.now() - new Date(page.last_edited_time).getTime() > ONE_MINUTE
287+
) {
281288
return cachedPage;
282289
} else {
283290
const normalisedPage = await this.normalisePage(page);

source/node.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
* -------------------------------------------------------------------------
1414
*/
1515

16+
import { createHash } from 'crypto';
17+
1618
import { name } from '#.';
1719

1820
import type { NodeInput, NodePluginArgs } from 'gatsby';
@@ -39,6 +41,7 @@ type NormalisedEntity<E extends FullEntity = FullEntity> = E extends any
3941
? Omit<E, 'parent'> & {
4042
parent: Link | null;
4143
children: Link[];
44+
digest: string;
4245
}
4346
: never;
4447

@@ -189,10 +192,7 @@ export class NodeManager {
189192
this.createNodeId(`${object}:${id}`),
190193
),
191194
internal: {
192-
contentDigest: this.createContentDigest({
193-
id: entity.id,
194-
lastEditedTime: entity.last_edited_time,
195-
}),
195+
contentDigest: entity.digest,
196196
...internal,
197197
},
198198
};
@@ -254,10 +254,7 @@ export class NodeManager {
254254

255255
for (const [id, newEntity] of newMap.entries()) {
256256
const oldEntity = oldMap.get(id);
257-
if (
258-
oldEntity &&
259-
oldEntity.last_edited_time !== newEntity.last_edited_time
260-
) {
257+
if (oldEntity && oldEntity.digest !== newEntity.digest) {
261258
updated.push(newEntity);
262259
}
263260
}
@@ -298,6 +295,7 @@ export function computeEntityMap(
298295
...entity,
299296
parent: normaliseParent(entity.parent),
300297
children: [],
298+
digest: createHash('sha256').update(JSON.stringify(entity)).digest('hex'),
301299
});
302300
}
303301

spec/node.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ describe('cl:NodeManager', () => {
241241
],
242242
});
243243
await manager.update([updatedDatabase, ...updatedDatabase.pages]);
244-
expect(createNode).toBeCalledTimes(1);
244+
expect(createNode).toBeCalledTimes(2);
245245
expect(deleteNode).toBeCalledTimes(1);
246246
});
247247
});

0 commit comments

Comments
 (0)