How to load more than 100 stories with the JS client?
- FAQ
- How to load more than 100 stories with the JS client?
Basic Setup:
// npm install storyblok-js-client
const StoryblokClient = require('storyblok-js-client')
// init with access token
const Storyblok = new StoryblokClient({
accessToken: 'YOUR_PREVIEW_TOKEN',
cache: {
clear: 'auto',
type: 'memory'
}
})
Option 1: According to specific amount of pages
let per_page = 100
let requests = []
let totalPages = 10 // 10 pages
for(let i = 1; i <= totalPages; i++) {
requests.push(Storyblok.get('cdn/stories/', {
per_page: per_page,
page: i
}))
}
let responses = await Promise.all(requests)
Option 2: All with head request
let per_page = 100
let requests = []
let initial = await Storyblok.client.head('cdn/stories/', { params: { per_page: 1, page: 1, token: Storyblok.accessToken } })
// access total header -> Total amount of Stories
// and calculate Total amount of pages
let totalPages = Math.ceil(initial.headers.total / per_page)
for(let i = 1; i <= totalPages; i++) {
requests.push(Storyblok.get('cdn/stories/', {
per_page: per_page,
page: i
}))
}
let responses = await Promise.all(requests)