Be sure to check out the whole .Net Core 3.1 with Kentico Kontent Series:
- .Net Core 3.1 with Kentico Kontent Part 1: Clean Architecture Design
- .Net Core 3.1 with Kentico Kontent Part 2: Clean Architecture Implementation
- .Net Core 3.1 with Kentico Kontent Part 3: How to Route and Link to Content
- .Net Core 3.1 with Kentico Kontent Part 4: Helpful Tips When Using Linked Items in Kentico Kontent
.NET CORE 3.1 WITH KENTICO KONTENT SERIES PART 4
At BizStream we are always striving to make high performing web apps.
Kontent is very helpful in its ease of use and speed.
Linked Items have some great uses, but can bloat your API responses. When performance is key we only want to be pulling down the needed information and nothing else.
The GIF above is a small example of how three related articles can add a lot of bloat. All of these other fields are unnecessary if you only need a couple of fields like the article title, and thumbnail. Let’s jump into some solutions to making your API calls leaner and faster.
Putting Limits on your Linked Items
Setting limits on your content types is a great way of preventing content admins from overloading pages with 10s of linked items or even more. The cogwheel has some very helpful settings in it. In this Related Articles example below I am allowing
At most 3 linked articles. When making a request for the main article I’m going to get back all of the data in these related articles too. If there wasn’t a limit you can see how this page could increase it’s load time pretty quickly.
The
Allowed content types prevent any content admins from adding whatever type they want as a “Related Article”. This ensures when I make a call to the
Delivery API will return exactly what is expected.
If a content admin tries to add more than 3 articles they are notified that this field is
Not filled in correctly.
Code Solutions
There are a few ways to manage your API response sizes with your code solution. There are a lot of
filters and parameters you can tag onto your API calls; let’s breakdown the method below.
- Starting on line 34 we are using the deliveryClient to get items that are of type KontentProductDetail. (We have some using aliases at the top declaring which ProductDetails is repository model vs our clean core model)
- Line 35 ensures we are retrieving the product detail the user is navigating to. The slug is coming from our ProductsController.
- Line 38 is limiting the response to only one content item. The LimitParameter could be set to 5 and if all the other conditions were met it would grab 5 products.
- Line 39 is pretty important to response size. This is how you actually get data from your linked items. Without setting the depth your linked item will return empty. Kentico recommends keeping the depth set as low as possible.
If your product detail has a related products section that displays at most three products’ title, image, and a short description then you might be better off making a service to make two separate calls to the API. If you plan on making two calls the method above wouldn’t need the
DepthParameter filter on the call because the related products will be added with the method below. Make sure you are not requesting unused data in your product detail request. The
ElementsParameter filter is very helpful in a situation like the related products by limiting the response to only the requested fields.
After making this call your service would append the related products to your product detail from the method above. So your service would end up looking something like this.
- Line 18 retrieves the base product detail info
- Line 19 gets the title, image, and description of the related products on that product detail.
In conclusion, limit your content admins so they can’t add 10s or more linked items in Kontent. In your code solution be sure to only request what is needed and if the content type is complex you might want to consider making a service and making multiple calls. Always remember to optimize your API calls to Kontent to make them that much faster.
Kimball Hospitality is a great example of how fast Kontent can be. Chrome Dev Tools Performance tab screenshot below speaks for itself. Sub one second for the home page is great!
Be sure to check out the whole .Net Core 3.1 with Kentico Kontent Series:
- .Net Core 3.1 with Kentico Kontent Part 1: Clean Architecture Design
- .Net Core 3.1 with Kentico Kontent Part 2: Clean Architecture Implementation
- .Net Core 3.1 with Kentico Kontent Part 3: How to Route and Link to Content
- .Net Core 3.1 with Kentico Kontent Part 4: Helpful Tips When Using Linked Items in Kentico Kontent