Skip to content

Commit

Permalink
fix: ignore cache if last_edit_time is within the last minute
Browse files Browse the repository at this point in the history
  • Loading branch information
alvis committed Jul 15, 2021
1 parent 40f4e64 commit ed7f863
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
9 changes: 8 additions & 1 deletion source/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -277,7 +279,12 @@ export class Notion {
const cacheKey = `page:${page.id}:content`;
const cachedPage = await this.cache.get<FullPage>(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);
Expand Down
14 changes: 6 additions & 8 deletions source/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
* -------------------------------------------------------------------------
*/

import { createHash } from 'crypto';

import { name } from '#.';

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

Expand Down Expand Up @@ -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,
},
};
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -298,6 +295,7 @@ export function computeEntityMap(
...entity,
parent: normaliseParent(entity.parent),
children: [],
digest: createHash('sha256').update(JSON.stringify(entity)).digest('hex'),
});
}

Expand Down
2 changes: 1 addition & 1 deletion spec/node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
Expand Down

0 comments on commit ed7f863

Please sign in to comment.