Netlify Create Cache

Netlify Create managed cache for documents, assets and schema originally provided by the content source modules.

Netlify Create's cache stores the documents and assets returned from the content source's getDocuments() and the getAssets() methods as well as the schema returned from the getSchema() method.

As mentioned earlier, content source modules should remain stateless with respect to the content and the schema they fetch from the underlying content sources. Once the content source is initialized, Netlify Create retrieves and caches the content and schema using instance methods such as getSchema, getDocuments, and getAssets. When a content source instance needs to access cached data, it can call methods on the cache object provided to the init() method. This approach ensures that Netlify Create and the content source instance operate on the same data and remain synchronized.

You can use the Cache type included in the @stackbit/types package to introspect its method signatures.

Cache Methods

The Cache object exposes the following methods to retrieve, update and delete cached documents and assets, as well as to invalidate the cached schema.

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
interface Cache<
  SchemaContext = unknown,
  DocumentContext = unknown,
  AssetContext = unknown,
> {
  getDocuments: () => Document<DocumentContext>[]
  getAssets: () => Asset<AssetContext>[]
  getSchema: () => Schema<SchemaContext>
  getDocumentById: (documentId: string) => Document<DocumentContext> | undefined
  getAssetById: (assetId: string) => Asset<AssetContext> | undefined
  getModelByName: (modelName: string) => Model | undefined
  updateContent: (
    contentChanges: ContentChanges<DocumentContext, AssetContext>,
  ) => Promise<void>
  invalidateSchema: () => void
}

getSchema

This method returns the schema originally returned by the content source's getSchema() method.

getDocuments

This method returns the documents originally returned by the content source's getDocuments() method.

getAssets

This method returns the assets originally returned by the content source's getSchema() method.

getModelByName(modelName: string)

This method returns a model by name, as originally returned in the models array by the content source's getSchema() method.

getDocumentById

This method returns a document by ID.

getAssetById

This method returns an asset by ID.

updateContent

This method updates the Netlify Create cache with new, updated, or deleted documents and/or assets.

invalidateSchema

invalidateSchema():

This method notifies Netlify Create to invalidate the cached schema.

Note that these methods may return an empty result if the cache method was called before Netlify Create had a chance to retrieve the data from the content source. For instance, if you call cache.getSchema() before Netlify Create calls the content source module's getSchema() method, you will receive an object with empty arrays.

ContentChanges

The ContentChanges object contains information about new, changed, or deleted documents and/or assets. This object includes the following optional properties:

  • documents: Array of created and updated Document objects.
  • assets: Array of created and updated Asset objects.
  • deletedDocumentIds: Array of deleted document ids.
  • deletedAssetIds: Array of deleted asset ids.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
interface ContentChanges<DocumentContext = unknown, AssetContext = unknown> {
  documents?: Document<DocumentContext>[]
  assets?: Asset<AssetContext>[]
  deletedDocumentIds?: string[]
  deletedAssetIds?: string[]
}