Manually download entries and assets into the SDK memory
Overview
When you build your React/NextJS application using Studio and Studio SDK, your app uses data from the Contentful Platform. As content editors keep creating and editing an experience, more and more data entries will be used in that experience. This also happens when the content editor binds entries to components, or binds visual assets to components.
This guide covers a special case: when your component has properties of type Link or Array which allow your component to accept not just a flat (or “shallow”) entity or an asset, but an entire “tree” starting with an entry and pulling its associated entries.
For example, you can have a component that pulls in entityOfTypeLink and a “tree” of data stemming from it (up to what we call “level-3”).
However, under certain circumstances, you may want to access data beyond “level-3”, meaning that you may want to have your entryOfTypeLink pull in a tree of data, which spans deeper then “level-3”.
Here’s an example of overfetching on simple “level-2” BlogPost entity and “level-3” Author entity:
When your React component that is designed to primarily display a blog post (BlogPost type entry) also needs to display the author’s name and picture (Author type entry), that author’s data will be prefetched by the SDK.
When the Author entry is downloaded, it will download all of the fields of that entry:
author.fields.title(scalar field)author.fields.profilePic(reference to asset)author.fields.homeTownPic(reference to asset)
Note, that homeTownPic is not used by your component, but it’s still loaded. In this simple example, the code is overfetching only one reference. However, it can be more complex if an entry has dozens of references.
You need to watch out for overfetching when manually preloading entities beyond “level-3”.
To prefetch the data you can manually use this method:
How to download additional entries when using NextJS
In your NextJS page that serves route for Studio-powered pages, usually you will be loading experiences using this pattern:
The Studio SDK will automatically fetch data up to “level-3” when called within getExperience().
However, if there’s a need to load deeper levels of Contentful data, you can use the following method. The logic of this method can be found in the utility method fetchAdditionalLevels() .
The essence of the utility is:
- extract leaf links from the experience using the
extractLeafLinksReferencedFromExperience()call - assuming the Experience is loaded by Studio SDK up to “level-3” entities, the leaf links will be the links to “level-4” entities
- recursively download (if you want apart from “level-4” also “level-5”) all of the entities
- insert those manually downloaded entities into the
inMemoryEntitiescache. This is the same cache where SDK stores all of the Contentful entries (up to “level-3”), which means that you can rely on the same entity resolution algorithm that usesinMemoryEntities.maybeResolveLink()function
client.withoutLinkResolution.getEntries() and not client.getEntries(), where the latter resolves references of the entries.- when you call
inMemoryEntities.addEntities(entities)the entities will be copied, and their copies will beObject.freeze'edrecursively, as per convention used for allinMemoryEntitiescache
And now you can use this fetchAdditionalLevels() within the page component:
It’s important to guard the call to fetchAdditionalLevels() like shown below, because this fetching may only work in Preview+Delivery mode (this early prefetching will not work when the experience is loaded in Studio IFRAME because in that mode, experience will be undefined even though there’s no error).
In this method, we prefetch up to additional 3 levels (as per first argument to fetchAdditionalLevels(3,…)). Note that if there’s no references going deep, the recursive fetching of additional levels may end early.
- “level-4”
- “level-5”
- “level-6”
If you want to only load additional single level, you can use
fetchAdditinalLevels(1,…) - “level-4”
So the only lines added to the page.tsx were:
and
How to download additional entries when using React CSR
The source code for early preloading of entities for CSR app can be found in Page.tsx.
The essence of this method revolves around calling the fetchAdditionalLevels() utility function.
The main changes are:
Here is the full code sample (or find it on github in Page.tsx.):