Continuation
Learn how to add pagination to a content extension.
In a content extension, there's a limit to how many resources—images, embeds, or containers—can be returned in a single response. This limit ensures that resources are loaded quickly.
To provide users with more resources than this limit allows, they need to be loaded across multiple requests. In some APIs, this is known as pagination. In the content extension API, it's known as continuation. The terms, however, can be used interchangeably.
The best way to understand pagination is with a practical example, so this topic explains how to implement pagination for Lorem Picsum, an API for placeholder photos. This API is free and registration is not required.

Prerequisites

This guide assumes experience with:
  • REST APIs
  • Node.js
  • Express.js
You should have also completed the quick start for content extensions.

Step 1: Set up a content extension

  1. 1.
    Create an app via the Developer Portal.
  2. 2.
    Add a content extension to the app.
  3. 3.
    Set up a REST API with Glitch and Express.js:
    1
    const express = require("express");
    2
    const app = express();
    3
    4
    app.use(express.json());
    5
    app.use(express.static("public"));
    6
    7
    // Routes go here
    8
    9
    app.listen(process.env.PORT || 3000);
    Copied!

Step 2: Load the initial page of resources

When a user opens a content extension, Canva sends a POST request to /content/resources/find:
1
app.post("/content/resources/find", async (request, response) => {
2
console.log(request.body);
3
});
Copied!
The purpose of this request is to retrieve the resources that will appear in the side panel.
To retrieve a list of images from Lorem Picsum, we can send a GET request to the following URL:
1
https://picsum.photos/v2/list
Copied!
This request will return an array of images, with each image represented by an object with the following structure:
1
{
2
"id": "0",
3
"author": "Alejandro Escamilla",
4
"width": 5616,
5
"height": 3744,
6
"url": "https://unsplash.com/...",
7
"download_url": "https://picsum.photos/..."
8
}
Copied!
To retrieve a different page of images, we can append a page parameter to the URL:
1
https://picsum.photos/v2/list?page=2
Copied!
To send HTTP requests, add the axios package to the project:
  1. 1.
    Open the package.json file.
  2. 2.
    Click Add Package.
  3. 3.
    Search for "axios."
  4. 4.
    Select the first result.
Then require the package at the top of the server.js file:
1
const axios = require("axios");
Copied!
To perform the request, call the request method:
1
const options = {
2
url: "https://picsum.photos/v2/list",
3
params: {
4
page: 1,
5
},
6
};
7
8
const picsum = await axios.request(options);
Copied!
Then convert the data into a format that Canva will understand:
1
const images = picsum.data.map((image) => {
2
return {
3
type: "IMAGE",
4
id: image.id,
5
name: `Photo by ${image.author}`,
6
url: image.download_url,
7
thumbnail: {
8
url: image.download_url,
9
},
10
contentType: "image/jpeg",
11
};
12
});
13
14
response.send({
15
type: "SUCCESS",
16
resources: images,
17
});
Copied!

Step 3: Provide the next page number in the response

Before a user reaches the end of the resource list, we want to trigger an additional request to /content/resources/find. This will allow us to seamlessly load additional resources, creating the effect of infinite scrolling.
To accomplish this, add a continuation property to the response:
1
response.send({
2
type: "SUCCESS",
3
resources: images,
4
continuation: "2",
5
});
Copied!
The presence of this property tells Canva that it should send another POST request to /content/resources/find before the user reaches the end of the list of resources. The value of this property will also be included in the body of that request.
In this example, the value of the continuation property is "2". This is because we've loaded the first page of results and we would like to load the second page. But this value does not have to be a page number. The only requirement is that this value is a string.
After making this change, scroll through the resources in the side panel.
The same resources will be repeatedly loaded. This isn't exactly what we want, but it's a step in the right direction.

Step 4: Get the current page number from the request body

When a continuation property is included in a response, that same continuation property will be made available in the body of the next POST request that's sent to /content/resources/find.
To see an example of this, log the value of request.body.continuation:
1
console.log(request.body.continuation);
Copied!
When the first page of resources is loaded, the continuation property is null. This is because there isn't a previous response. But if you trigger another request by scrolling through the resources, the value of "2" will be logged. You can use this behavior to load unique resources for each request.
At the top of the route, before the options object, create a currentPage variable that contains the value of the request body's continuation property:
1
const currentPage = request.body.continuation;
Copied!
But since the continuation property will be null on the initial request, provide a fall-back value of "1":
1
const currentPage = request.body.continuation || "1";
Copied!
Then update the page parameter in the options object to use this currentPage variable:
1
const options = {
2
url: "https://picsum.photos/v2/list",
3
params: {
4
page: currentPage,
5
},
6
};
Copied!
Based on these changes, we'll be able to load the first and second pages of content into the side panel.
If we keep scrolling through the resources though, it's now the second page of images that will be repeatedly loaded. This is because we've hard-coded the value of the continuation property in all responses as "2".
Even so, this is another step in the right direction.

Step 5: Increment the page number

To load unique resources for every request, we need to increment the value of the continuation property.
Before the response.send method, create a variable that:
  1. 1.
    Parses the number from the currentPage variable.
  2. 2.
    Increments the page number by one.
  3. 3.
    Converts the result back into a string.
This is an example of how to do that:
1
const nextPage = (parseInt(currentPage, 10) + 1).toString();
Copied!
Then provide this variable in the response:
1
response.send({
2
type: "SUCCESS",
3
resources: images,
4
continuation: nextPage,
5
});
Copied!
Based on this change, a unique set of resources will be loaded for each request.

Step 6: Load the final page of resources

When the final page of resources is reached, don't provide a continuation property in the response:
1
response.send({
2
type: "SUCCESS",
3
resources: images,
4
});
Copied!
...or set the continuation property to null:
1
response.send({
2
type: "SUCCESS",
3
resources: images,
4
continuation: null,
5
});
Copied!
This signifies that there's no more pages to load.

Example

1
const axios = require("axios");
2
const express = require("express");
3
const app = express();
4
5
app.use(express.json());
6
app.use(express.static("public"));
7
8
app.post("/content/resources/find", async (request, response) => {
9
const currentPage = request.body.continuation || "1";
10
11
const options = {
12
url: "https://picsum.photos/v2/list",
13
params: {
14
page: currentPage,
15
},
16
};
17
18
const picsum = await axios.request(options);
19
20
const images = picsum.data.map((image) => {
21
return {
22
type: "IMAGE",
23
id: image.id,
24
name: `Photo by ${image.author}`,
25
url: image.download_url,
26
thumbnail: {
27
url: image.download_url,
28
},
29
contentType: "image/jpeg",
30
};
31
});
32
33
const nextPage = (parseInt(currentPage, 10) + 1).toString();
34
35
response.send({
36
type: "SUCCESS",
37
resources: images,
38
continuation: nextPage,
39
});
40
});
41
42
app.listen(process.env.PORT || 3000);
Copied!
Last modified 18d ago