-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnuxt.generate.js
72 lines (63 loc) · 2.57 KB
/
nuxt.generate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const axios = require('axios');
// From: https://www.storyblok.com/faq/how-to-generate-routes-for-nuxt-js-using-storyblok
module.exports = function generateStoryblokUrls(token) {
return {
routes: function(callback) {
const per_page = 10;
const version = 'published';
let cache_version = 0;
let page = 1;
// other routes that are not in Storyblok with their slug.
let routes = ['/']; // adds home directly but with / instead of /home
// Load space and receive latest cache version key to improve performance
axios.get(`https://api.storyblok.com/v1/cdn/spaces/me?token=${token}`).then(space_res => {
// timestamp of latest publish
cache_version = space_res.data.space.version;
// Call first Page of the Stories
axios
.get(
`https://api.storyblok.com/v1/cdn/stories?token=${token}&version=${version}&per_page=${per_page}&page=${page}&cv=${cache_version}`
)
.then(res => {
res.data.stories.forEach(story => {
if (story.full_slug != 'home') {
routes.push('/' + story.full_slug);
}
});
// Check if there are more pages available otherwise execute callback with current routes.
const total = res.headers.total;
const maxPage = Math.ceil(total / per_page);
if (maxPage <= 1) {
callback(null, routes);
return;
}
// Since we know the total we now can pregenerate all requests we need to get all stories
let contentRequests = [];
for (let page = 2; page <= maxPage; page++) {
contentRequests.push(
axios.get(
`https://api.storyblok.com/v1/cdn/stories?token=${token}&version=${version}&per_page=${per_page}&page=${page}`
)
);
}
// Axios allows us to exectue all requests using axios.spread we will than generate our routes and execute the callback
axios
.all(contentRequests)
.then(
axios.spread((...responses) => {
responses.forEach(response => {
response.data.stories.forEach(story => {
if (story.full_slug != 'home') {
routes.push('/' + story.full_slug);
}
});
});
callback(null, routes);
})
)
.catch(callback);
});
});
}
};
};