File tree Expand file tree Collapse file tree 3 files changed +15
-10
lines changed Expand file tree Collapse file tree 3 files changed +15
-10
lines changed Original file line number Diff line number Diff 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 */
7577export 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 ) ;
Original file line number Diff line number Diff line change 1313 * -------------------------------------------------------------------------
1414 */
1515
16+ import { createHash } from 'crypto' ;
17+
1618import { name } from '#.' ;
1719
1820import 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
Original file line number Diff line number Diff 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 } ) ;
You can’t perform that action at this time.
0 commit comments